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?
Asked
Active
Viewed 2.7e+01k times
7 Answers
77
What do you mean by "make sure"?
alert('message');
window.location = '/some/url';
redirects user after they click OK in the alert window.
penartur
- 9,654
- 5
- 37
- 48
52
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
}
Joe
- 15,441
- 4
- 43
- 81
-
with `if(confirm("sometext","defaultvalue"))` I think is enought – David Diez Feb 22 '12 at 11:50
-
Meant to put `confirm`, but my brain abandoned me and I wrote `prompt` - fixed it a you were posting that lol – Joe Feb 22 '12 at 11:53
19
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
David Diez
- 1,105
- 10
- 23
14
I think what you need is this :
if(confirm("Do u want to continue?")) {
window.location.href = "/some/url"
}
Subodh
- 2,198
- 16
- 22
7
Yes, simply redirect right after the alert() call:
alert('blah blah');
location.href = '....';
ThiefMaster
- 298,938
- 77
- 579
- 623
2
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
}
}
});
yotke
- 1,065
- 2
- 12
- 25
0
Response.Write("<script Response.Write("<script
language='javascript'>window.alert('Done');window.location='URL';</script>");
Momen Alnaser
- 75
- 6
-
5This question is nearly 8 years old and it has an accepted answer. – Trenton McKinney Oct 07 '19 at 01:05