我有一个如下所示的页面,代码类似
<a id="goog" onclick="return togooglepage()" href="#">Google.com</a> <br/>
<a id="yahoo" onclick="return toyahoopage()" href="#">Yahoo.com</a> <br/>
function warning(){
if(true){
return "You are leaving the page";
}
}
window.onbeforeunload = warning;
function togooglepage()
{
window.location = "www.google.co.in";
}
function toyahoopage()
{
window.location = "www.yahoo.com";
}
我有window.onbeforeunload =警告以防止关闭,例如点击浏览器上的十字按钮或点击Ctrl + W。
当上述任何行动发生时
我想执行
function warning(){
if(true){
return "You are leaving the page";
}
}
window.onbeforeunload = warning;
但是,当点击超级链接时,我怎么能跳过这个?
是否有可能避免这种情况?
答案 0 :(得分:4)
试试这个:
function togooglepage()
{
window.onbeforeunload = null;
window.location = "www.google.co.in";
}
function toyahoopage()
{
window.onbeforeunload = null;
window.location = "www.yahoo.com";
}
基本上你想要的是在window.location之前将window.onbeforeunload设置为null
答案 1 :(得分:1)
设置一些全局变量以避免警告,例如:
function warning(){
if (!warning.disabled) return "You are leaving the page"
}
window.onbeforeunload = warning;
function togooglepage() {
warning.disabled = true
window.location = "www.google.co.in"
}