繁体   English   中英

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

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

你好我只需要关闭浏览器/标签时需要弹出确认消息,而不是在点击任何链接时。 在我的代码中,当我关闭浏览器/选项卡以及点击任何我不想要的链接时,它会弹出消息。 我真的不知道该怎么做。 谁能帮帮我吗。 我的代码是 -

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

尝试这个:-

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

你说的事件onbeforeunload工作得很好。

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

看到它在行动。

TL /博士

下面的代码将是完全伪代码,描述了您需要运行以使其工作的方法。


第一步是您需要为站点内的所有“内部”链接添加标识符。 例如,如果您的页面是联系人,并且主页上有指向该页面的链接,则需要区分页面类型。

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")
            }
        }
    });

});

以上是我之前使用过的,它完美无缺。 基本上它的作用是为内部链接(站点内的链接)添加一个属性,然后当用户浏览你的站点时, onbeforeunload运行confirmExit()函数并检查internal_link是否为false,如果是,则显示消息否则它浏览网站。

没有什么对我有用,但我用上面的代码和其他论坛的一些组合做了。

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

提交表单时也会触发onbeforeunload事件,因此我们需要阻止该事件。

暂无
暂无

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

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