我是JavaScript的初学者。
我听说当一个警告框弹出时,用户必须点击"确定"。继续。
然而,这个警告框演示(jsfiddle)并没有停止在OK?
<html>
<body>
<button onclick='alert(window.location="https://www.google.com");'>Google</button>
</body>
</html>
使用提示用户确认的confirm
,而不是alert
:
const button = document.querySelector('button');
button.addEventListener('click', function(){
var confirmed = confirm("Redirect?");
if(confirmed){
window.location="https://www.google.com";
}
})
<html>
<body>
<button>Google</button>
</body>
</html>