TheTime Posted February 25, 2014 Report Posted February 25, 2014 Mi s-a ivit de curand necesitatea de a impiedica un tab sa isi schimbe locatia atunci cand este in background. Prin magia atotputernicului Copy-Paste de prin diferite surse, am obtinut urmatorul cod javascript care isi cam face treaba:var focused = true; //variabila in care retinem daca tabul curent are sau nu focus// putea fi folosit si document[hidden], dar am avut ceva probleme cu el in iframe// Set the name of the hidden property and the change event for visibilityvar hidden, visibilityChange;if (typeof document.hidden !== "undefined") { // Opera 12.10 and Firefox 18 and later support hidden = "hidden"; visibilityChange = "visibilitychange";} else if (typeof document.mozHidden !== "undefined") { hidden = "mozHidden"; visibilityChange = "mozvisibilitychange";} 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]) { focused = false; } else { focused = true; }}if (typeof document.addEventListener === "undefined" || typeof hidden === "undefined") { alert("This demo requires a browser, such as Google Chrome or Firefox, that supports the Page Visibility API.");} else { // Handle page visibility change document.addEventListener(visibilityChange, handleVisibilityChange, false);}var leave_message = "Please be aware, you are about to be redirected to another location!";//functie care dorim sa se execute atunci cand pagina este navigata, dar nu are focus//atentie la if-ul din interiorfunction goodbye(e){ if(!window.top.document.hasFocus() && !focused) { if(!e) { e = window.event; } //e.cancelBubble is supported by IE - this will kill the bubbling process. e.cancelBubble = true; e.returnValue = leave_message; //e.stopPropagation works in Firefox. if (e.stopPropagation) { e.stopPropagation(); e.preventDefault(); } //return works for Chrome and Safari return leave_message; }}window.onbeforeunload=goodbye;Sursa principala: Page visibility APINu este mare lucru, dar poate preveni atacuri de acest gen. Quote
Silviu Posted February 25, 2014 Report Posted February 25, 2014 Felicitari! Chestia asta o bagi in consola, nu? Sau cum o folosesti, ca daca e site-ul tau nu ii inteleg rostul Quote
TheTime Posted February 25, 2014 Author Report Posted February 25, 2014 Am facut-o pentru un site de-al meu. Rostul ei e fix sa nu permita paginii mele sa fie navigata din exterior fara ca utilizatorul sa vada acest lucru.Daca vrei sa o folosesti pentru un alt site, sa zicem facebook.com, poti folosi un addon care iti permite sa rulezi scripturi personale: greasemonkey pentru firefox, tampermonkey pentru chrome. Quote