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");
        }
    }
}
dfsq
  • 191,768
  • 25
  • 236
  • 258
user2132849
  • 81
  • 1
  • 1
  • 2
  • Side note: the `keepGoing` variable is rather unnecessary. – JJJ Mar 09 '13 at 07:28
  • 1
    `email.indexOf('http://') === -1` should be `email.indexOf('http://') !=0`, because you must validate http:// is the first part of website address – Iswanto San Mar 09 '13 at 07:33

3 Answers3

3

use location.href

window.location.href = email;
Tim Sommer
  • 419
  • 2
  • 15
1
window.location.href = email;

should redirect the user to the URL contained in email

Stuart M
  • 11,458
  • 6
  • 45
  • 59
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;
         }       

    }
 }
Iswanto San
  • 18,263
  • 13
  • 58
  • 79