2013-03-09 157 views
9

URL重定向Javascript? 我试图用http://重定向输入的网址,然后提示,直到找到http://,如果找到它,它会将它引导到输入的网站。URL重定向Javascript

继承人到目前为止我的代码:

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"); 
     } 
    } 
} 
+0

备注:'keepGoing'变量是不必要的。 – JJJ 2013-03-09 07:28:08

+1

'email.indexOf('http://')=== -1'应该是'email.indexOf('http://')!= 0',因为你必须验证http://是第一部分的网站地址 – 2013-03-09 07:33:04

回答

1
window.location.href = email; 

应该将用户重定向到包含在email

0

的URL可以设置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; 
     }  

    } 
} 
3

使用location.href

window.location.href = email;