Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript &引用;“离开网站”;警觉的_Javascript_Alert - Fatal编程技术网

Javascript &引用;“离开网站”;警觉的

Javascript &引用;“离开网站”;警觉的,javascript,alert,Javascript,Alert,我的网站上有以下href: a href="http://www.google.com" name="external" onclick="confirmExit(this.name) 如果用户不想通过单击“取消”(下面的代码不完整)离开,我就是想不出如何让他/她留在我的网站上 jQuery/Ajax不是一个选项。只需返回false即可 function confirmExit(name){ if(name == "external"){ if(!confirm("Go

我的网站上有以下href:

a href="http://www.google.com" name="external" onclick="confirmExit(this.name)
如果用户不想通过单击“取消”(下面的代码不完整)离开,我就是想不出如何让他/她留在我的网站上

jQuery/Ajax不是一个选项。

只需返回false即可

function confirmExit(name){
    if(name == "external"){
        if(!confirm("Go to external site?")){
            return false;
        }
    }
}
如果您希望在所有链接上发生这种情况,或者甚至在用户关闭选项卡时发生这种情况,请签出。

只需返回false即可

function confirmExit(name){
    if(name == "external"){
        if(!confirm("Go to external site?")){
            return false;
        }
    }
}

如果您希望在所有链接上,甚至在用户关闭选项卡时都发生这种情况,请退出。

您不需要函数。我建议你使用这个:

$(function () {
     $('a[name="external"]').click(function () {
         if (!confirm("Go to external site?")) {
            event.preventDefault();
         }
     });
});
如果您使用它,您不需要将onclick=“confirmExit(this.name)”添加到所有内容中。只需添加以上内容,您的工作就会完成

如果需要JSFIDLE的行为,可以这样做:

window.onbeforeunload = function(){ return "Are you sure you want to leave the page?"; }

你不需要函数。我建议你使用这个:

$(function () {
     $('a[name="external"]').click(function () {
         if (!confirm("Go to external site?")) {
            event.preventDefault();
         }
     });
});
如果您使用它,您不需要将onclick=“confirmExit(this.name)”添加到所有内容中。只需添加以上内容,您的工作就会完成

如果需要JSFIDLE的行为,可以这样做:

window.onbeforeunload = function(){ return "Are you sure you want to leave the page?"; }
用于在用户离开页面之前显示确认对话框

$(window).bind("beforeunload", function() {
   if(true) { //display confirm dialog?
       return "Are you sure you want to leave?";
   }
});
用于在用户离开页面之前显示确认对话框

$(window).bind("beforeunload", function() {
   if(true) { //display confirm dialog?
       return "Are you sure you want to leave?";
   }
});

confirmExit()
函数中使用
return false
,并在内联javascript中使用
return

HTML

<a href="http://www.google.com" name="external" onclick="return confirmExit(this.name)">Some Link</a>
                                  //Use return here also ---^

confirmExit()
函数中使用
return false
,并在内联javascript中使用
return

HTML

<a href="http://www.google.com" name="external" onclick="return confirmExit(this.name)">Some Link</a>
                                  //Use return here also ---^

如果用户导航出您的页面,他很可能想离开您的页面……基本上:
If(!confirm('foo')){return false;}
onclick=“return confirmExit();”
。从onclick返回false会有效地禁用单击。只是停下来说我讨厌这样的弹出窗口,你也不敢重新措辞说“单击取消以留在这个站点”@tymeJV,尽管如果用户从你的页面导航出去,它们很少有用(gmail、JSFIDLE…),他很可能想离开你的页面……基本上:
if(!confirm('foo')){return false;}
onclick=“return confirmExit();”
。从onclick返回false会有效地禁用单击。只是顺便说一下我讨厌这样的弹出窗口,你也不敢重新措辞说“单击取消以留在这个站点”@tymeJV,尽管它们很少有用(gmail、JSFIDLE…),假设参数
name
实际上设置为“外部”,从事件返回false应该可以防止它发生。@Teemu我还没有测试过这一点,但是在单击时返回false应该可以防止单击事件发生,这反过来应该可以防止onload事件发生,对吗?这就像一个符咒,因为我只想要name=external的HREF弹出窗口“。假设参数
name
实际设置为“external”,则从事件返回false应可防止其发生。@Teemu我没有测试过这一点,但在单击时返回false应可防止单击事件发生,这反过来会阻止onload事件的发生,对吗?这就像一个符咒,因为我只想看到name=external“.jquery/ajax不是OPjquery的选项/ajax不是OP的选项