如何在使用javascript出错时重新加载页面

How to reload page upon error with javascript?

本文关键字:新加载 加载 出错 javascript      更新时间:2023-09-26

有时我在加载html/javascript页面时遇到以下错误之一:

Failed to load resource: the server responded with a status of 403 (Forbidden)
Failed to load resource: net::ERR_CONNECTION_CLOSED 

但是,刷新后,它加载正常。有没有办法,如果页面在javascript中出现此错误,我可以让页面自动刷新

本质上,我希望页面在发生任何错误时刷新。

我会把你的精力集中在尝试解决 403 问题上,而不是试图检测和刷新。 如果这是我认为的那样,则通过强制客户端刷新,您可能会使问题变得更糟。 如果您在 apache 上安装了mod_evasive或类似的东西,它可能会返回 403,因为它一次检测到太多请求。

http://pr0gr4mm3r.com/internet/how-to-fix-random-403-errors/

在使用 jQuery 执行 ajax 请求之前使用它

$( document ).ajaxError(function(event, jqxhr) {
  if(jqxhr.status ===403) window.location = '/login/?error=forbidden';
});
你可以

试试"尝试"

<!DOCTYPE html>
<html>
<body>
  <p id="demo"></p>
<script>
 try {
  //code
 }
 catch(err) {
  location.reload();
 }
</script>

你可以这样做:为 img 标记放置错误事件,如果发生错误,则重新加载 img(甚至页面)。

<img id="myimg" src="image.jpg" onerror="img_load_error()"/>
<script>
function img_load_error() {
  var myimg = document.getElementById('myimg');
  var img_src = myimg.src;
  myimg.src='';
  myimg.src = img_src;
  //OR       
  //if you want to reload the page then just do:
  window.location.reload();
}
</script>