【发布时间】:2018-02-26 22:57:57
【问题描述】:
遇到了麻烦。我希望页面在您离开页面时刷新然后回来。起初我让它适用于 chrome 和 firefox,但不适用于 ie 和 edge。然后我又对其进行了一些调整,让它在边缘工作。即是唯一一个有问题的。不管我做什么,即进入一个无限循环的刷新。我什至把东西放进去让它只发生一次,比如在jquery中使用“one”函数,这就是它开始在edge中工作的方式,但我并不关心那个规则。然后我尝试了一种哈希技术,只有在 url 中没有哈希时才会这样做,所以它会刷新添加哈希,然后下一次加载不会刷新,因为有哈希,但这只能工作一次。我不知道如何重置哈希,所以它可以再次发生。我想出了如何让它每次都反转,所以它在没有哈希时会这样做,然后下一次是在哈希时这样做,但它同样适用于除 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.
【问题讨论】:
-
如果可能的话,把它贴成一个sn-p,这样我们就可以看到当前代码运行了
-
从未做过 sn-ps。不知道怎么办。
-
在创建或回答问题时使用工具栏中的
<>按钮,您可以在其中添加html、js、css等。然后代码将在此处运行 -
考虑使用
location.hash来检查和设置散列...并将散列设置为诸如“noreload”之类的东西。请注意,您在if语句的两个分支中都执行了location.reload(true),这可能就是为什么存在无限循环... -
查看当前的 sn-p。没有哈希方法。只需刷新窗口焦点。适用于chrome、ff和opera。边缘参差不齐,并闯入即。我也清理了这个问题。
标签: javascript jquery cross-browser