Javascript 如何从一个 URL 重定向到另一个 URL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8824141/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:41:06  来源:igfitidea点击:

How to redirect from one URL to another URL?

javascripthtml

提问by User

How can I redirect to another URL in a web page using JavaScript?

如何使用 JavaScript 重定向到网页中的另一个 URL?

回答by Paul

window.location.href = "URL2"

inside a JS block on the page or in an included file; that's assuming you really want to do it on the client. Usually, the server sends the redirect via a 300-series response.

在页面上的 JS 块内或在包含的文件中;那是假设您真的想在客户端上执行此操作。通常,服务器通过 300 系列响应发送重定向。

回答by David

Since you tagged the question with javascriptand html...

既然你用javascript和标记了问题html......

For a purely HTML solution, you can use a metatag in the headerto "refresh" the page, specifying a different URL:

对于纯 HTML 解决方案,您可以使用 中的meta标记header来“刷新”页面,指定不同的 URL:

<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.yourdomain.com/somepage.html">

If you can/want to use JavaScript, you can set the location.hrefof the window:

如果你能/要使用JavaScript,您可以设置location.hrefwindow

<script type="text/javascript">
    window.location.href = "http://www.yourdomain.com/somepage.html";
</script>

回答by Maniruzzaman Akash

You can redirect anything or more URL via javascript, Just simple window.location.hrefwith if else

您可以通过JavaScript,只是简单的输出重定向或多个URLwindow.location.hrefif else

Use this code,

使用此代码,

<script>
if(window.location.href == 'old_url')
{
    window.location.href="new_url";
}


//Another url redirect
if(window.location.href == 'old_url2')
{
    window.location.href="new_url2";
}
</script>

You can redirect many URL's by this procedure. Thanks.

您可以通过此过程重定向许多 URL。谢谢。

回答by Maniruzzaman Akash

If you want to redirect, just use window.location. Like so:

如果要重定向,只需使用window.location. 像这样:

window.location = "http://www.redirectedsite.com"

回答by Jeff

Why javascript?

为什么是 JavaScript?

http://www.instant-web-site-tools.com/html-redirect.html

http://www.instant-web-site-tools.com/html-redirect.html

<html>
<meta http-equiv="REFRESH" content="0;url=http://www.URL2.com"> 
</html>

Unless I'm missunderstanding...

除非我误解...

回答by jigish desai

location.href = "Pagename.html";

回答by locrizak

you can also use a meta tag to redirect to another url.

您还可以使用元标记重定向到另一个 url。

<meta http-equiv="refresh" content="2;url=http://webdesign.about.com/">

<meta http-equiv="refresh" content="2;url=http://webdesign.about.com/">

http://webdesign.about.com/od/metataglibraries/a/aa080300a.htm

http://webdesign.about.com/od/metataglibraries/a/aa080300a.htm