每次人离开(窗口模糊)然后返回(窗口焦点)时,如何进行页面刷新?

时间:2018-02-23 20:47:12

标签: javascript jquery cross-browser

遇到麻烦。当你离开页面然后回来我希望页面刷新。起初我让它适用于chrome和firefox,但不是ie和edge。然后我调整了一些,让它在边缘工作。即是唯一有问题的人。无论我做什么,即进入无限循环的刷新。我甚至把它放进去让它只发生一次,就像使用" one"在jquery中的函数,它是如何开始在边缘工作,但即没有关心该规则。然后我尝试了一种散列技术,只有在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.
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

这适用于所有浏览器:

window.addEventListener('focus', function(){
    console.log("focus");

});

window.addEventListener('blur', function(){     
    document.location.reload(); 
    console.log('leave');
});

答案 1 :(得分:0)

使用以下内容...

  public class MyDefaultSmsReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle intentExtras = intent.getExtras();

        if (intentExtras != null) {
          //read and manage received SMS
        }
    }
  }