Redirect from an HTML page

2018-12-31 02:58发布

Is it possible to set up a basic HTML page to redirect to another page on load?

25条回答
梦该遗忘
2楼-- · 2018-12-31 03:07

I would use both meta, and JavaScript code and would have a link just in case.

<!DOCTYPE HTML>
<html lang="en-US">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="refresh" content="0; url=http://example.com">
        <script type="text/javascript">
            window.location.href = "http://example.com"
        </script>
        <title>Page Redirection</title>
    </head>
    <body>
        <!-- Note: don't tell people to `click` the link, just tell them that it is a link. -->
        If you are not redirected automatically, follow this <a href='http://example.com'>link to example</a>.
    </body>
</html>

For completeness, I think the best way, if possible, is to use server redirects, so send a 301 status code. This is easy to do via .htaccess files using Apache, or via numerous plugins using WordPress. I am sure there are also plugins for all the major content management systems. Also, cPanel has very easy configuration for 301 redirects if you have that installed on your server.

查看更多
无与为乐者.
3楼-- · 2018-12-31 03:07

I found a problem while working with a jQuery Mobile application, where in some cases my Meta header tag wouldn't achieve a redirection properly (jQuery Mobile doesn't read headers automatically for each page so putting JavaScript there is also ineffective unless wrapping it in complexity). I found the easiest solution in this case was to put the JavaScript redirection directly into the body of the document, as follows:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta http-equiv="refresh" content="0;url=myURL" />
    </head>

    <body>
        <p>You are not logged in!</p>
        <script language="javascript">
            window.location = "myURL";
        </script>
    </body>
</html>

This seems to work in every case for me.

查看更多
一个人的天荒地老
4楼-- · 2018-12-31 03:08

Use this code:

<meta http-equiv="refresh" content="0; url=https://google.com/" />

Please pay attention: put it in the head tag.

查看更多
呛了眼睛熬了心
5楼-- · 2018-12-31 03:09

You can auto redirect by HTTP Status Code 301 or 302.

For PHP:

<?php
    Header("HTTP/1.1 301 Moved Permanently");
    Header("Location: http://www.redirect-url.com");
?>
查看更多
一个人的天荒地老
6楼-- · 2018-12-31 03:11

Try using:

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

Note: Place it in the head section.

Additionally for older browsers if you add a quick link in case it doesn't refresh correctly:

<p><a href="http://example.com/">Redirect</a></p>

Will appear as

Redirect

This will still allow you to get to where you're going with an additional click.

查看更多
栀子花@的思念
7楼-- · 2018-12-31 03:12

You don't need any JavaScript code for this. Write this in the <head> section of the HTML page:

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

As soon as the page loads at 0 seconds, you can go to your page.

查看更多
登录 后发表回答