Detecting where a window is unloading to
So on my site I load a webpage in an iframe, and some sites have frame busting code to break out of the iframe and redirect the top window to their page. I want to use the onbeforeunload event to determine where the webpage is being redirected to. Is there a function that effectively lets me do event.get_redirect_location
or something along those lines?
Lots of research has led me to the conclusion that there is no way JavaScript can check what the 'next location' is going to be and compare the two domains, if the window.location
was set FROM the iframe.
This is the only way I know of for preventing an iframe from breaking out of the site. First, you set your onbeforeunload
function to act as normal and warn the user he/she is leaving your site. Then - this is the critical part - you add code to your internal links to prevent page changes from triggering the onbeforeunload event. This means that the onbeforeunload
event will ONLY fire if the user is leaving your domain :
(function(){
window.onbeforeunload = function()
{
if( !/bnagged=trueb/.test( document.cookie ) )
{
document.cookie = "nagged=true";
location.assign('http://www.mysite.com');
return "You are being redirected off our site!";
}
}
var lnk;
for(var i in document.links)
{
if( (lnk = document.links[i] ).href.indexOf( document.domain ) > -1 )
{
lnk.onclick = function()
{
window.onbeforeunload = null;
}
}
}
})();
链接地址: http://www.djcxy.com/p/59036.html
上一篇: 如果检测到帧检测器,则重定向
下一篇: 检测窗口正在卸载的位置