本文介绍了离开现场时使用确认方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望在单击外部链接时弹出确认消息.虽然它不想解决.当我单击外部链接时,我没有收到确认弹出窗口.有解决方案吗?
I want a confirm message to popup when clicking on an external link. Though it doesnt want to work out. I dont get the confirm popup when i click on an external link. Any solutions ?
JavaScript代码
Javascript code
function warning(){
var warning = confirm('Do you want to leave');
if(!warning){
alert("Staying on site");
return false;
} else {
alert("Leaving site");
return true;
}
}
function init() {
var warn = document.getElementsByTagName("ul");
for(i=0; i<warn.length; i++)
if(warn[i].className == "meny"){
var link = document.getElementsByTagName("li");
}
for(i=0; i<link.length; i++){
var links = document.getElementsByTagName("a");
if(links[i].className == "external"){
links.onclick = warning;
}
}
}
HTML代码:
<ul class="meny"">
<li><a class="external" href="https://www.mah.se">Mah</a></li>
<li><a class="external" href="https://www.google.se/">Google</a></li>
<li><a class="external" href="http://www.facebook.com/">Facebook</a></li>
<li><a class="external" href="http://www.youtube.com/">Youtube</a></li>
<li><a href="#">Digitalt</a></li>
<li><a href="#">Kultur</a></li>
<li><a href="#">Nöje</a></li>
<li><a href="#">Sport</a></li>
<li><a href="#">Familj</a></li>
<li><a href="#">Resor</a></li>
<li><a href="#">Inrikes</a></li>
<li><a href="#">Utrikes</a></li>
<li><a href="#">Jobb</a></li>
</ul>
推荐答案
只能通过处理onbeforeunload
事件来完成此行为.您的处理程序应返回一个字符串,这是显示给用户的消息.一个简单的例子:
This behavior can only be accomplished by handling the onbeforeunload
event. Your handler should return a string, which is the message that appears to the user. A simple example:
window.onbeforeunload = function () {
return 'are you sure you want to leave?';
};
这篇关于离开现场时使用确认方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!