Redirecting to another page meta tag

2019-07-04 22:01发布

问题:

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.

回答1:

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.



回答2:

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



回答3:

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>


回答4:

GIYF: H76: Using meta refresh to create an instant client-side redirect



回答5:

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>