Verificare accesare alta fereastra in pagina

Discutii si intrebari legate de scripturi si functii JavaScript, jQuery si Ajax, cod JavaScript in general.
Stefan
Mesaje:117

Verificare accesare alta fereastra in pagina

Salut, se poate verifica daca un user a accesat alt tab (fereastra in browser) ?
Imagine: imgur.com/a/D2n4a
- (nu ma refer la asta: Detect when a user leaves a web page ).

MarPlo Mesaje:4343
Salut
Din ceea ce stiu, in afara de raspunsurile de la acea pagina, nu cunosc vreo metoda precisa ca sa stii cand utilizatorul acceseaza alta fereastra in browser cu alt site.
- Chiar daca incerci vreun script JS care sa verifice schimbarea pozitiei mouseului la fiecare secunda, nu va indica clar accesarea altei ferestre.

Stefan Mesaje:117
Am revenit cu un raspuns, tot am cautat pana cand intr-un sfarsit am gasit, chiar daca a trecut ceva timp de la crearea acestui topic.

Cod: Selectaţi tot

<script>
	
// "librairie" de gestion de la visibilité
//  var visible = vis(); // donne l'état courant
//  vis(function(){});   // définit un handler pour les changements de visibilité
var vis = (function(){
	var stateKey, eventKey, keys = {
		hidden: "visibilitychange",
		webkitHidden: "webkitvisibilitychange",
		mozHidden: "mozvisibilitychange",
		msHidden: "msvisibilitychange"
	};
	for (stateKey in keys) {
		if (stateKey in document) {
			eventKey = keys[stateKey];
			break;
		}
	}
	return function(c) {
		if (c) {
			document.addEventListener(eventKey, c);
			//document.addEventListener("blur", c);
			//document.addEventListener("focus", c);
		}
		return !document[stateKey];
	}
})();

vis(function(){
	document.title = vis() ? 'Visible' : window.location = 'test';;
	console.log(new Date, 'visible ?', vis());
});

// to set the initial state
document.title = vis() ? 'Visible' : 'Not visible';

</script>

MarPlo Mesaje:4343
Interesant acel script. M-am jucat putin cu el, i-am facut cateva modificari, si a rezultat acesta:

Cod: Selectaţi tot

<script>
// Defines a handler for page visibility changes
var vis_chg = (function(){
  var stateKey, eventKey, keys = {
    hidden: 'visibilitychange',
    webkitHidden: 'webkitvisibilitychange',
    mozHidden: 'mozvisibilitychange',
    msHidden: 'msvisibilitychange'
  };
  for(stateKey in keys){
    if(stateKey in document){
      eventKey = keys[stateKey];
      break;
    }
  }
  return function(c){
    if(c){
      document.addEventListener(eventKey, c);
      //document.addEventListener('focus', c); //for when focus again this page
    }
    return !document[stateKey];
  }
})();

// Gives the initial state
var page_vstate = vis_chg() ?'visible' :'not-visible';

vis_chg(function(){
  var re_vchg = vis_chg(); //FALSE=leaves the page; TRUE=re-enter in page
  console.log(new Date, 'vis_chg: ', re_vchg); //for debug in console
  page_vstate = re_vchg ?'visible' : whenVisChg();
});

//This function is executed when the page changes visibility state
function whenVisChg(){
  document.title ='not-visible';  //changes page title in browser

  //you can set your code here
  alert('page visibility state changed');

  return 'not-visible';
}
</script>

Subiecte similare