[英]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.';
};
下面的代碼將是完全偽代碼,描述了您需要運行以使其工作的方法。
第一步是您需要為站點內的所有“內部”鏈接添加標識符。 例如,如果您的頁面是聯系人,並且主頁上有指向該頁面的鏈接,則需要區分頁面類型。
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.