URL重定向JavaScript

8

URL重定向Javascript?我试图重定向一个带有http://的输入网址,并提示直到找到http://,如果找到它,它将直接转到输入的网站。

这是我目前的代码:

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

旁注:keepGoing变量相当不必要。 - JJJ
1
email.indexOf('http://') === -1 应该改为 email.indexOf('http://') !=0,因为你必须验证 http:// 是否是网站地址的第一部分。 - Iswanto San
3个回答

3
使用 location.href
window.location.href = email;

1
window.location.href = email;

应该将用户重定向到email中包含的URL。

0

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

    }
 }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接