Redirecting to another page meta tag

2019-07-04 21:12发布

If I want to redirect to another page in my HTML file, do in have to place the meta tag in the head or can I place it at the top of the file before the DOCTYPE? Thank you.

5条回答
狗以群分
2楼-- · 2019-07-04 21:47

W3C deprecates the use it, but they do offer an example on W3C:

<HEAD>
    <TITLE>Don't use this!</TITLE>
    <META http-equiv="refresh" content="5;http://www.example.com/newpage">
</HEAD>
<BODY>
    <P>If your browser supports Refresh, you'll be transported to our
    <A href="http://www.example.com/newpage">new site</A>
    in 5 seconds, otherwise, select the link manually.
</BODY>
查看更多
甜甜的少女心
3楼-- · 2019-07-04 22:03

The meta tag has to be inside the <head></head> section. You can not add anything before <!DOCTYPE html>

Here is detailed description of DOCTYPE

查看更多
孤傲高冷的网名
5楼-- · 2019-07-04 22:07

You can't place a meta tag above the DOCTYPE. The DOCTYPE must always be the first element in an HTML document, and meta tags must only be placed in the head.

Documents must consist of the following parts, in the given order:

  • Optionally, a single "BOM" (U+FEFF) character.
  • Any number of comments and space characters.
  • A DOCTYPE.
  • Any number of comments and space characters.
  • The root element, in the form of an html element.
  • Any number of comments and space characters.

Source: http://www.w3.org/TR/html5/syntax.html#writing

For purposes of this question, the spec says that a document must start with a DOCTYPE and be followed by a root html element. While a meta tag might still work, there is no guarantee of it doing so today and continuing to do so in the future.

查看更多
放我归山
6楼-- · 2019-07-04 22:07

You should insert the following line in the head section of your HTML page, replacing http:example.com/ with the actual web page to which you want to redirect your viewers:

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

Here is an example with the correct line inserted in a typical HTML page. Note that it comes above the title tag.

<html>
<head>
<meta http-equiv="refresh" content="2;url=http://example.com" />
<title>Page Moved</title>
</head>
<body>
This page has moved. Click <a href="http://www.example.com">here</a> to go to the new page.
</body>
</html>
查看更多
登录 后发表回答