Jump to content
TheTime

Prevent window redirect when unfocused

Recommended Posts

Posted

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 visibility

var 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 interior

function 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 API

Nu este mare lucru, dar poate preveni atacuri de acest gen.

Posted

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...