如何从HTML页面重定向?


页面重定向是指你点击一个URL试图访问页面X,但实际上你被导向了另一个页面Y。这是由于页面重定向造成的。网站设计师在需要更改特定网站的布局或特定页面的位置时会用到重定向。

要从HTML页面进行重定向,我们使用META标签。同时,我们也使用http-equiv属性来为content属性的值提供一个HTTP头部。content中的值是以秒为单位的时间;页面将在指定时间后重定向。

如果要立即加载,请将content属性设置为0。否则,如果要稍微延迟重定向,请指定以秒为单位的时间。

语法

<meta http-equiv="refresh" content="time; URL=new_url" />

我们可以看到,重定向需要两个参数:

  • 时间:表示浏览器将用户重定向到不同页面之前的延迟。

  • 新URL:表示延迟后我们需要将用户重定向到的URL地址。

以下是一些示例……

示例

在下面的示例中,我们通过在<meta>标签中指定URL链接来将页面重定向到另一个页面。

<!DOCTYPE html> <html> <head> <title>HTML Redirect</title> <meta http-equiv="refresh" content="5; url = https://tutorialspoint.com/index.htm" /> </head> <body> <h1>WELCOME TO Tutorialspoint</h1> <p>you'll be redirected to Tutorialspoint Homepage, in 5 seconds.</p> </body> </html>

运行代码后,浏览器窗口会打开并等待5秒钟,然后自动重定向到tutorials point主页。

延迟HTML页面重定向

如果在重定向到另一个页面或页面重定向延迟时遇到任何错误,这可能是由于使用了旧的浏览器造成的。为了解决这个问题,我们将提供一个新的网页链接。

<!DOCTYPE html> <html> <head> <meta http-equiv="refresh" content="5; URL=https://tutorialspoint.com/index.htm" /> </head> <body> <p>If you are not redirected in five seconds, <a href="https://tutorialspoint.com/index.htm">click here</a>.</p> </body> </html>

执行上述脚本后,网页会显示我们使用<a href>提到的链接,以避免重定向延迟。

更新于:2023年9月10日

31K+ 次浏览

启动您的职业生涯

完成课程后获得认证

开始学习
广告
© . All rights reserved.