2013-03-09 16 views
9

URLリダイレクトJavascript? http://が入力され、http://が見つかるまで入力されたWebアドレスをリダイレクトしようとしています。見つかった場合は、入力されたWebサイトにそのWebアドレスを誘導します。URLリダイレクトJavascript

HERESに私のコードは、これまで:

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

+1

'email.indexOf( 'http://')=== -1'は' email.indexOf( 'http://')!= 0'でなければなりません。ウェブサイトアドレスの –

答えて

1
window.location.href = email; 

はあなたがwindow.location.hrefを設定することができますemail

0

に含まれるURLにユーザーをリダイレクトする必要があります。

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;