2016-11-06 84 views
0
<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <title> 
     </title> 
     <meta charset="utf-8" /> 
     <link rel="stylesheet" type="text/css" href="css/custom.css" /> 
    </head> 
    <body> 
      <a href="http://www.google.co.in">Google</a> 
    <script type="text/javascript" src="js/jquery-3.1.1.min.js" ></script> 
    <script type="text/javascript" src="js/custom.js" ></script> 
    </body> 
</html> 

之前没有表现出以下是custom.js警报卸载

$(window).on('beforeunload',function(){ 
    alert("abc"); 
}); 

我希望页面之前,点击链接后,将下一个页面显示报警代码,但它没有显示。

+0

*我希望页面在点击链接后进入下一页之前显示警告* - 为什么?这对用户来说非常烦人和不友善,这就是浏览器禁用它的原因。如果您声明您尝试使用此警报解决的问题,也许我们可以帮助您找到更好的方法来实现您的目标。 – Ryan

+0

嗯,我刚刚通过jQuery教程,这就是为什么我想这样做。 –

+0

签出[此](http://stackoverflow.com/a/1270421/5549391)答案和答案中的链接 – agDev

回答

1

铬块警示,因为他们选择这样做。

你可以听链接的点击事件,并抛出一个确认,像这样:

var link = document.querySelectorAll('a.confirmation')[0]; 
 

 
link.onclick = function() { 
 
    var check = confirm('Are you sure ?'); 
 
    if (check === true) { 
 
     return true; 
 
    } 
 
    else { 
 
     return false; 
 
    } 
 
}
<a href="http://www.google.co.in" class="confirmation">Google</a>

或使用jQuery

$(".confirmation").on('click', function() { 
 
    var check = confirm('Are you sure ?'); 
 
    if (check === true) { 
 
     return true; 
 
    } 
 
    else { 
 
     return false; 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<a href="http://www.google.co.in" class="confirmation">Google</a>

3

beforeunload效果很好,但在你的情况下,存在因使用被阻断BU浏览器的警报没有任何影响,所以你可以在控制台打印,但你不能使用警报在beforeunload

上面的例子我使用过控制台。在beforeunload

$(window).on("beforeunload",function(event){ 
 
    console.log("This works , but alert is blocked "); 
 
    
 
    // alert(""); this wont work and it's blocked by the browser due to window's object missing 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<a href="http://www.google.co.in">Google</a>