James Kevin De Jesus
James Kevin De Jesus

Reputation: 19

JavaScript automatic redirect

I'm writing a code in JavaScript wherein if the condition is null/false, the browser automatically redirects to another page. The only question is what are the codes for this block? Please help.

if(condition==false){
//redirect code
}
else
continue;

Upvotes: 0

Views: 410

Answers (5)

Muhammmad Hadi Rajani
Muhammmad Hadi Rajani

Reputation: 611

window.location = "URL TO REDIRECT";

or

window.location.replace("URL TO REDIRECT");

or

window.location.assign("URL TO REDIRECT");

or

window.location.href = "URL TO REDIRECT";

Remember to include "http://" or "https://" with your url. :)

Upvotes: 1

Alone
Alone

Reputation: 313

try this tested!

 window.location.assign("URL");

e.g.

window.location.assign("http://www.google.com");

Upvotes: 0

Mr. Bhosale
Mr. Bhosale

Reputation: 3096

:) use code bellow

               if( condition== null /false ) 
            { 
                 alert(" Ohhooo.. Redirected ");
                 window.location.replace('http://url');


                // another code
                // location.href = "url.html";


                // u can also use time out code after time page redirect
                //setTimeout("location.href = 'http://url';",1500);


            }

Upvotes: 0

Kirill V.
Kirill V.

Reputation: 67

if(condition==false){
window.location.replace("http://stackoverflow.com");
}

Upvotes: 1

kazimt9
kazimt9

Reputation: 563

// try this
window.location = 'YOUR_URL';

Upvotes: 3

Related Questions