如果按下警告,请在确定按钮后转到URL

Ond*_*rej 45 javascript alert url-redirection

我需要确保当用户在JavaScript警报窗口中单击"确定"时,浏览器将移动到其他URL.这可能吗?

pen*_*tur 58

" 确定 " 是什么意思?

alert('message');
window.location = '/some/url';
Run Code Online (Sandbox Code Playgroud)

在警报窗口中单击"确定"后重定向用户.


Joe*_*Joe 37

我怀疑你的意思是在一个confirm窗口(即是/否选项).

if (window.confirm('Really go to another page?'))
{
    // They clicked Yes
}
else
{
    // They clicked no
}
Run Code Online (Sandbox Code Playgroud)


Dav*_*iez 15

警报不返回值,事实上返回undefined所以我现在找到的最简单的方法是调整这样的警报

if(!alert("my text here")) document.location = 'http://stackoverflow.com/';
Run Code Online (Sandbox Code Playgroud)

更好的方法是使用这样的confirm()javascript函数

if(confirm("my text here")) document.location = 'http://stackoverflow.com/';
Run Code Online (Sandbox Code Playgroud)

另一个选择当然是制作自己的警报


Sub*_*odh 8

我想你需要的是:

if(confirm("Do u want to continue?")) {
    window.location.href = "/some/url"
}
Run Code Online (Sandbox Code Playgroud)

  • 这是纯金。 (2认同)

Thi*_*ter 6

是的,只需在alert()通话后立即重定向:

alert('blah blah');
location.href = '....';
Run Code Online (Sandbox Code Playgroud)