如何从 HTML 页面重定向?
front end technologyhtmlweb development更新于 2025/3/25 9:16:17
页面重定向是指您点击 URL 到达页面 X,但在页面内部却被定向到另一个页面 Y。这种情况是由于页面重定向造成的。网站设计师在需要更改特定网站的布局或特定页面的位置时会依赖重定向。
要从 HTML 页面重定向,我们使用 META 标签。此外,我们还使用 http-equiv 属性为 content 属性的值提供 HTTP 标头。content 中的值是您希望页面在多少秒后重定向。
如果您希望立即加载,请将 content 属性设置为 0。否则,如果您想稍微延迟重定向,请指定一个以秒为单位的时间。
语法
<meta http-equiv="refresh" content="time; URL=new_url" />
我们可以观察到,重定向需要两个参数 -
Time:表示浏览器将用户重定向到其他页面之前的延迟时间。
New_url:表示延迟后我们需要将用户重定向到的 URL 地址。
以下是示例……
示例
在下面的示例中,我们通过在 <meta> 标签中指定 URL 链接将页面重定向到另一个页面。
<!DOCTYPE html> <html> <head> <title>HTML Redirect</title> <meta http-equiv="refresh" content="5; url = https://www.cainiaomax.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 秒后自动重定向到教程主页
延迟重定向 HTML 页面
如果我们在重定向到另一个页面时遇到任何错误,或者重定向页面有时会延迟,这可能是因为我们使用的是旧版浏览器。尽管如此,我们还是会提供一个指向该网页的新链接。
<!DOCTYPE html> <html> <head> <meta http-equiv="refresh" content="5; URL=https://www.cainiaomax.com/index.htm" /> </head> <body> <p>If you are not redirected in five seconds, <a href="https://www.cainiaomax.com/index.htm">click here</a>.</p> </body> </html>
执行上述脚本时,网页会出现一个我们提到使用 <a href> 的链接,以避免重定向延迟。
相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

