8

URL Redirect Javascript? I have am trying to redirect an entered web address with a http:// and prompt until a http:// is found and if it is found it will direct it to the website that is entered.

Heres my code so far:

function enter() {
    var keepGoing = true;
    var email;
    while (keepGoing) {

        keepGoing = false

        email = prompt("Please Enter Website Address");
        // Website Address validation to see if it has http://
        if (email.indexOf('http://') === -1) {
            alert("Not valid Web Address");
            keepGoing = true;
        }

        // if nothing was entered
        else if (email == '') {
            var email = prompt("Please Enter Valid Web Address");
        }
    }
}
2
  • Side note: the keepGoing variable is rather unnecessary.
    – JJJ
    Mar 9, 2013 at 7:28
  • 1
    email.indexOf('http://') === -1 should be email.indexOf('http://') !=0, because you must validate http:// is the first part of website address Mar 9, 2013 at 7:33

3 Answers 3

3

use location.href

window.location.href = email;
1
window.location.href = email;

should redirect the user to the URL contained in email

0

You can set window.location.href:

function enter()
{
    var keepGoing = true;
    var email;
    while(keepGoing){

        keepGoing = false

        email = prompt("Please Enter Website Address");
        // Website Address validation to see if it has http://
         if(email.indexOf('http://') != 0)
         {
            alert("Not valid Web Address");
            keepGoing = true;
         }

         //if nothing was entered
         else if(email == '')
         {
            var email = prompt("Please Enter Valid Web Address");
         }

         else
         {
            window.location.href=email;
         }       

    }
 }

Not the answer you're looking for? Browse other questions tagged or ask your own question.