Go to URL after OK button if alert is pressed

后端 未结 7 548
逝去的感伤
逝去的感伤 2020-12-01 17:33

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?

相关标签:
7条回答
  • 2020-12-01 18:20

    I think what you need is this :

    if(confirm("Do u want to continue?")) {
        window.location.href = "/some/url"
    }
    
    0 讨论(0)
  • 2020-12-01 18:20
    Response.Write("<script Response.Write("<script 
    language='javascript'>window.alert('Done');window.location='URL';</script>");
    
    0 讨论(0)
  • 2020-12-01 18:23

    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
    }
    
    0 讨论(0)
  • 2020-12-01 18:23

    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

    0 讨论(0)
  • 2020-12-01 18:24

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

    alert('blah blah');
    location.href = '....';
    
    0 讨论(0)
  • 2020-12-01 18:27

    What do you mean by "make sure"?

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

    redirects user after they click OK in the alert window.

    0 讨论(0)
提交回复
热议问题