如何使用JavaScript在网页中将页面重定向到另一个URL?
如何使用JavaScript在网页中将页面重定向到另一个URL?
window.location.href = "URL2"
如果你确实希望在客户端上执行此操作,可以在页面的JS块或包含文件中执行;通常情况下,服务器会通过300系列的响应发送重定向。
URL2
网页时会发生什么?我想要重定向网页如果用户加载了URL1
。 - return你在问题标签中添加了javascript
和html
...
对于一个纯粹的HTML解决方案,你可以在head
标签中使用一个meta
标签来“刷新”页面,指定不同URL:
<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.yourdomain.com/somepage.html">
如果您可以/希望使用JavaScript,您可以设置window
的location.href
:
<script type="text/javascript">
window.location.href = "http://www.yourdomain.com/somepage.html";
</script>
您可以通过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。谢谢。
window.location
。用法如下:window.location = "http://www.redirectedsite.com"
http://www.instant-web-site-tools.com/html-redirect.html
<html>
<meta http-equiv="REFRESH" content="0;url=http://www.URL2.com">
</html>
location.href = "Pagename.html";
您还可以使用元标签将页面重定向到另一个URL。
<meta http-equiv="refresh" content="2;url=http://webdesign.about.com/">
http://webdesign.about.com/od/metataglibraries/a/aa080300a.htm