location.reload() doesn't work on remote server

你。 提交于 2019-12-13 07:53:51

问题


I have a javascript animation which uses delay() and setInterval() functions. Everything works fine but animation elements become crazy when user opens another browser tab and goes back to the previous tab.

As a solution I need to reload the page each time a user is visited the animation tab again and it seems the solution is here.

But I see that reloading page works only on localhost. The same code doesn't work on remote server i.e. page is not reloaded by revisiting the page.

Here is the code:

var hidden, visibilityChange; 
if (typeof document.hidden !== "undefined") {
  hidden = "hidden";
  visibilityChange = "visibilitychange";
} else if (typeof document.msHidden !== "undefined") {
  hidden = "msHidden";
  visibilityChange = "msvisibilitychange";
} else if (typeof document.webkitHidden !== "undefined") {
  hidden = "webkitHidden";
  visibilityChange = "webkitvisibilitychange";
}

function handleVisibilityChange() {
  if (!document[hidden]) {location.reload();}
};

document.addEventListener(visibilityChange, handleVisibilityChange, false);

回答1:


I cannot explain, but it seems that soluton is

location.href = location.href;

instead of

location.reload();

The page is reloaded on local and remote server.



来源:https://stackoverflow.com/questions/41040456/location-reload-doesnt-work-on-remote-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!