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

发布于 2025-01-08 04:19:48 字数 61 浏览 0 评论 0原文

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

I need to make sure that when the user clicks OK in a JavaScript alert window, the browser moves to a different URL. Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

心凉 2025-01-15 04:19:49

我想你需要的是这样的:

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

I think what you need is this :

if(confirm("Do u want to continue?")) {
    window.location.href = "/some/url"
}
折戟 2025-01-15 04:19:49

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

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

Yes, simply redirect right after the alert() call:

alert('blah blah');
location.href = '....';
时光病人 2025-01-15 04:19:49

如果是为了可访问性,并且您想监听页面上的每个链接,而不是检查您是否将当前站点离开到另一个域,请查看我写的内容,扩展乔的答案

        $('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
                }
            }
        }); 

If it is for accessibility and you want to listen to every link on the page and than check if you are leaving the current site to another domain, check out what I wrote, expanding on Joe's answer

        $('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
                }
            }
        }); 
最冷一天 2025-01-15 04:19:49

就我而言,还需要重新加载。

alert('your message');
window.location = '/yoururl';
window.location.reload();

In my case, reload was also needed.

alert('your message');
window.location = '/yoururl';
window.location.reload();
我喜欢麦丽素 2025-01-15 04:19:49
Response.Write("<script Response.Write("<script 
language='javascript'>window.alert('Done');window.location='URL';</script>");
Response.Write("<script Response.Write("<script 
language='javascript'>window.alert('Done');window.location='URL';</script>");
看海 2025-01-15 04:19:48

确保”是什么意思?

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

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

What do you mean by "make sure"?

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

redirects user after they click OK in the alert window.

瀟灑尐姊 2025-01-15 04:19:48

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

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

I suspect you mean in a confirm window (ie. Yes/No options).

if (window.confirm('Really go to another page?'))
{
    // They clicked Yes
}
else
{
    // They clicked no
}
未蓝澄海的烟 2025-01-15 04:19:48

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

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

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

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

另一个选择是让你的当然是自己的警报

An alert does not return a value, in fact returns undefined so the easiest way I find right now is conditioning the alert like this

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

A better way is using confirm() javascript function like this

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

Another option is making your own alert of course

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文