我希望在单击外部链接时弹出确认消息。虽然它不想解决。单击外部链接时,我没有收到确认弹出窗口。有什么解决办法吗?
Javascript代码
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>