遇到这个问题了。我希望当您离开页面然后返回时刷新页面。起初我让它适用于 Chrome 和 Firefox,但不适用于 IE 和 Edge。然后我又对其进行了一些调整,让它在边缘工作。 ie 是唯一有问题的。无论我做什么,ie都会进入刷新的无限循环。我什至把一些东西放进去,让它只发生一次,比如在 jquery 中使用“one”函数,这就是它在 Edge 中开始工作的方式,但 ie 并不关心这个规则。然后我尝试了一种哈希技术,它仅在 url 中没有哈希的情况下才执行,因此它会刷新添加哈希,然后下一次加载不会刷新,因为有哈希,但这只有效一次。我不知道如何重置哈希,以便它可以再次发生。我想出了如何让它每次都反转,所以它在没有哈希时执行,然后下一次在哈希时执行,但它再次适用于除 ie 之外的所有浏览器。 ie不断刷新。这是所有代码以及我尝试注释掉的所有不同内容。我不需要在离开时刷新,只需回来即可。每次回来。
$(document).one('ready',function(){
$(window).one('load',function(){
$(window).blur(function(e)
//$(window).one('blur',function(e)
{
// Do Blur Actions Here
console.log('left'); //test
//if(window.location.hash) {
// # exists in URL
//if (typeof window.history.replaceState == 'function') {
//history.replaceState({}, '', window.location.href.slice(0, -1));
//}
//}
});
$(window).focus(function(e)
//$(window).one('focus',function(e)
{
// Do Focus Actions Here
console.log('came back'); //test
//if(document.URL.indexOf("#")==-1){ //Check if the current URL contains '#'
//console.log('no hash');
//url = document.URL+"#"; // use "#". Add hash to URL
//location = "#";
//location.reload(true); //Reload the page
//console.log(url);
//}
//else {
//console.log('has hash');
//if (typeof window.history.replaceState == 'function') {
//if (history.replaceState({}, '', window.location.href.slice(0, -1))){
//url = document.URL;
//location.reload(true);
//console.log(url);
//}
//}
//}
location.reload(true);
});
//$(function(){
//$(window).bind('blur', function(){
//console.log('window blur');
//});
//$(window).bind('focus', function(){
//console.log('window focus');
//});
// IE EVENTS
//$(document).bind('focusout', function(){
//console.log('document focusout');
//});
//$(document).bind('focusin', function(){
//console.log('document focusin');
//location.reload(true);
//$(document).unbind( "focusin", handler );
//$(document).unbind( "focusout", handler );
//$(document).unbind( "blur", handler );
//$(document).unbind( "focus", handler );
//});
//});
//if(window.location.hash) {
//window.location.href.substr(0, window.location.href.indexOf('#'));
//window.location.href.split('#')[0];
//console.log('has hash');
//}
//history.pushState("", document.title, window.location.pathname);
// remove fragment as much as it can go without adding an entry in browser history:
//window.location.replace("#");
// slice off the remaining '#' in HTML5:
//if (typeof window.history.replaceState == 'function') {
//history.replaceState({}, '', window.location.href.slice(0, -1));
//}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
page stuff.
click outside this area then back in this area over and over. that simulates leaving and coming back.
最佳答案
这应该适用于所有浏览器:
window.addEventListener('focus', function(){
console.log("focus");
});
window.addEventListener('blur', function(){
document.location.reload();
console.log('leave');
});
关于javascript - 如何让每次人们离开(窗口模糊)然后返回(窗口焦点)时发生页面刷新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48955996/