简体   繁体   English

仅在关闭标签/窗口时弹出确认框

[英]pop up of confirmation box when closing tab/window only

Hello i need a pop up message of confirmation when going to close browser/tab only, not when going to click on any link. 你好我只需要关闭浏览器/标签时需要弹出确认消息,而不是在点击任何链接时。 In my code, it gave me pop up message when i close browser/tab as well as click on any link, which i don't want. 在我的代码中,当我关闭浏览器/选项卡以及点击任何我不想要的链接时,它会弹出消息。 I really have no idea how to do it. 我真的不知道该怎么做。 Can anyone please help me. 谁能帮帮我吗。 My code is- 我的代码是 -

<body>
    <div id="copyright">
    <div class="moduletable copyright ">


<div class="custom copyright">
    <p>
<span class="copy">© Copyright 2008</span>
 Inc. All rights reserved. </p>
</div>
    </div>
  </div>
</div>
    </div>
        <br class="clear" />
  </div>
</div>
<script language="JavaScript">
window.onbeforeunload = bunload;
function bunload(){
dontleave="Are you sure you want to leave?";
return dontleave;
}
</script>
</body>
</html>

Try this:- 尝试这个:-

 <body>
    <a href="demo-1" onclick="prevent()">click here</a>
    <script language="JavaScript">

        window.onbeforeunload = function () {
            return "Are you sure?";
        }
        function prevent() {
            window.onbeforeunload = function () { };
        };

    </script>
</body>

Your said event onbeforeunload works perfectly. 你说的事件onbeforeunload工作得很好。

window.onbeforeunload = function(e) {
  return 'Dialog text here.';
};

See it in action. 看到它在行动。

Tl/Dr TL /博士

The below code will be completely pseudo-code describing the method you'll need to run through to get this to work. 下面的代码将是完全伪代码,描述了您需要运行以使其工作的方法。


The first step is that you'll need to add an identifier to all "internal" links within your site. 第一步是您需要为站点内的所有“内部”链接添加标识符。 For example, if your page is contact, and there is a link to that page form your home page, you need to distinguish between the types of pages. 例如,如果您的页面是联系人,并且主页上有指向该页面的链接,则需要区分页面类型。

function confirmExit() {
    if (!internal_link) {
        // random shit..
        return message
    }
}
var key = "internal_link";
window[key] = false;
var message = "Your custom message. Are you sure you want to blah?";
window.onbeforeunload = confirmExit;
internal_link = false;
$(document).ready(function () {
    if (!window.location.origin) {
        window.location.origin = window.location.protocol + "//" + window.location.hostname
    }

    $("a").each(function () {
        if ($(this)[0].href.indexOf(window.location.origin) == 0) {
            if ($(this).attr("onclick") == "undefined") {
                $(this).attr("onclick", key + " = true")
            } else {
                if ($(this).attr("onclick")) {
                    var e = $(this).attr("onclick");
                    $(this).attr("onclick", e + " " + key + " = true")
                } else {
                    $(this).attr("onclick", key + " = true")
                }
            }
        } else {
            if ($(this).attr("onclick") !== key + " = true" || $(this).attr("onclick") == "undefined") {
                $(this).attr("onclick", key + " = false")
            }
        }
    });

});

The above is what I've used previously and it's worked perfectly. 以上是我之前使用过的,它完美无缺。 Basically what it does is adds an attribute to internal links (links within your site), then when users navigate around your site, the onbeforeunload runs the confirmExit() function and checks if the internal_link is false, if so, it shows the message otherwise it navigates through the site. 基本上它的作用是为内部链接(站点内的链接)添加一个属性,然后当用户浏览你的站点时, onbeforeunload运行confirmExit()函数并检查internal_link是否为false,如果是,则显示消息否则它浏览网站。

Nothing work for me but I made it with some combinations of the above code and from other forums. 没有什么对我有用,但我用上面的代码和其他论坛的一些组合做了。

<script language="JavaScript">

        window.onbeforeunload = function () {
            return "Are you sure?";
        };

        $(document).ready(function () {
            $('a').click(function () {
                window.onbeforeunload = null;
            });
            $('form').submit(function () {
                window.onbeforeunload = null;
            });
        });
    </script>

The onbeforeunload event will be fired when you submit a form too, so we need to prevent the event. 提交表单时也会触发onbeforeunload事件,因此我们需要阻止该事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM