URL 重定向 Javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15308343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
URL Redirect Javascript
提问by user2132849
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.
URL 重定向 Javascript?我正在尝试使用 http:// 重定向输入的网址,并提示直到找到 http://,如果找到,它会将其定向到输入的网站。
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");
}
}
}
回答by Tim Sommer
use location.href
使用 location.href
window.location.href = email;
回答by Stuart M
window.location.href = email;
should redirect the user to the URL contained in email
应该将用户重定向到包含在 email
回答by Iswanto San
You can set window.location.href:
您可以设置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;
}
}
}