2013-03-09 159 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;