如果按下警报,请转到“确定”按钮后的URL

Go to URL after OK button if alert is pressed

本文关键字:确定 按钮 URL 如果      更新时间:2023-09-26

我需要确保当用户在JavaScript警报窗口中单击"确定"时,浏览器会移动到不同的URL。这可能吗?

"确保"是什么意思?

alert('message');
window.location = '/some/url';

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

我怀疑您的意思是在confirm窗口中(即"是/否"选项)。

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

警报不返回值,实际上返回undefined,所以我现在找到的最简单的方法是像一样调节警报

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

更好的方法是使用confirm()javascript函数,如

if(confirm("my text here")) document.location = 'http://stackoverflow.com/';

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

我认为您需要的是:

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

是的,只需在alert()调用后立即重定向即可:

alert('blah blah');
location.href = '....';

如果是为了访问,并且你想收听页面上的每个链接,然后检查你是否要将当前网站转移到另一个域,请查看我写的内容,扩展Joe的答案

        $('a').on('click', function() {
            if ( this.host !== window.location.host ) {
                if ( window.confirm('Really go to another page?') ) {
                    // They clicked Yes
                    console.log('you chose to leave. bye.');
                }
                else {
                    // They clicked no
                    console.log('you chose to stay here.');
                    return false
                }
            }
        }); 

在我的案例中,还需要重新加载。

alert('your message');
window.location = '/yoururl';
window.location.reload();
Response.Write("<script Response.Write("<script 
language='javascript'>window.alert('Done');window.location='URL';</script>");