Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

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

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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...