如何使用JavaScript重定向到网页中的另一个URL?如何从一个URL重定向到另一个URL?
15
A
回答
22
4
如果你想重定向,只需使用window.location
。像这样:
window.location = "http://www.redirectedsite.com"
0
您还可以使用元标记重定向到另一个网址。
<meta http-equiv="refresh" content="2;url=http://webdesign.about.com/">
http://webdesign.about.com/od/metataglibraries/a/aa080300a.htm
17
既然你标记javascript
和html
问题...
对于一个纯粹的HTML的解决方案,你可以使用一个meta
标签在header
,以 “刷新”页面,指定不同的URL:
<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.yourdomain.com/somepage.html">
如果你能/要使用JavaScript,您可以设置location.href
的window
的:
<script type="text/javascript">
window.location.href = "http://www.yourdomain.com/somepage.html";
</script>
2
为什么要使用JavaScript?
http://www.instant-web-site-tools.com/html-redirect.html
<html>
<meta http-equiv="REFRESH" content="0;url=http://www.URL2.com">
</html>
除非我missunderstanding ...
1
location.href = "Pagename.html";
6
您可以通过JavaScript重定向任何东西或多个URL,只是简单的用window.location.href
if else
使用此代码,
<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>
您可以通过此过程重定向多个URL。谢谢。
不适用于HTML或JavaScript。您发送带有301或302状态码的HTTP位置标题。具体情况取决于您的网络服务器和/或服务器端编程环境。 – Quentin 2012-01-11 17:53:58