Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go to URL after OK button if alert is pressed

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?

like image 981
Ondrej Avatar asked Feb 22 '12 11:02

Ondrej


People also ask

How do I handle Alert OK button?

Selenium has multiple APIs to handle alerts with an Alert interface. To click on the Ok button on alert, first of all we have to switch to alert with switchTo(). alert() method. Next, to click on the Ok button, we have to use accept() method.


4 Answers

What do you mean by "make sure"?

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

redirects user after they click OK in the alert window.

like image 187
penartur Avatar answered Oct 22 '22 23:10

penartur


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
}
like image 28
Joe Avatar answered Oct 22 '22 21:10

Joe


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

like image 20
David Diez Avatar answered Oct 22 '22 21:10

David Diez


I think what you need is this :

if(confirm("Do u want to continue?")) {
    window.location.href = "/some/url"
}
like image 15
Subodh Avatar answered Oct 22 '22 23:10

Subodh