Go to URL after OK button if alert is pressed

JavascriptAlertUrl Redirection

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

What do you mean by "make sure"?

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

redirects user after they click OK in the alert window.

Solution 2 - Javascript

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
}

Solution 3 - Javascript

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

Solution 4 - Javascript

I think what you need is this :

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

Solution 5 - Javascript

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

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

Solution 6 - Javascript

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
        		}
        	}
        }); 

Solution 7 - Javascript

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOndrejView Question on Stackoverflow
Solution 1 - JavascriptpenarturView Answer on Stackoverflow
Solution 2 - JavascriptJoeView Answer on Stackoverflow
Solution 3 - JavascriptDavid DiezView Answer on Stackoverflow
Solution 4 - JavascriptSubodhView Answer on Stackoverflow
Solution 5 - JavascriptThiefMasterView Answer on Stackoverflow
Solution 6 - JavascriptyotkeView Answer on Stackoverflow
Solution 7 - JavascriptMomen AlnaserView Answer on Stackoverflow