开发者

why html meta refresh redirection doesn't work on the prompt page?

开发者 https://www.devze.com 2023-03-24 14:22 出处:网络
Please kindly consider the following code: <html> <head> <title>This is the from page</title>

Please kindly consider the following code:

<html>
<head>
        <title>This is the from page</title>
</head>
<body>
    <script type='text/javascript'>
        function redirect(destination)
        {
            newWindow 开发者_如何学Go= window.open(destination, "_blank", "width=790,height=520");
            newWindow.document.write("<meta http-equiv='refresh' content='4;url=" + destination + "'>");
            newWindow.document.write("<h1>Now redirecting to destination page in 4 seconds...</h1>");
        }

        redirect("./to.html");
    </script>
</body>

Basically I can only see the prompt page with Now redirecting to destination page in 4 seconds... displayed. But it gets stuck in there forever... Firebug tells me that the meta tag does exist in the prompt page.

Any thoughts? Thanks a lot in advance!


    function redirect(destination)
    {
        setTimeout(function(){window.location = destination;},4000);
    }


OK, maybe I get what you're doing; you want to show a "redirecting" page while a new page is being loaded. It may be easiest if you create a brand new HTML page that has the sole purpose of doing redirects. The redirect URL could be added to the query string. Here's how you could create that redirect page (we'll assume this is called redirect.html):

<html>
  <head>
    <script>

      // Parse the query string to get the destination URL
      var params = {};
      var pairs = window.location.search.substring(1).split('&');
      for (var i = 0; i < pairs.length; i++) {
          var components = pairs[i].split('=');
          params[components[0]] = decodeURIComponent(components[1]);
      }

      setTimeout(function() { window.location = params.redirect; }, 4000);

    </script>
  </head>
  <body>
    <h1>Now redirecting to destination page in 4 seconds...</h1>
  </body>
</html>

And here's how you'd use it in your host page:

function redirect(destination)
{
    window.open(
        'redirect.html?redirect=' + encodeURIComponent(destination), "_blank",
        "width=790,height=520");
}

redirect("./to.html");


I think the reason your code isn't working is that the browser has already processed the page and its metadata. Assuming that's the case, adding a redirect after the fact won't work. (Someone who knows more about browser internal would need to verify that.)

I'm not sure why you would run this instead of just loading the page, but one approach that should work is to set a javascript timeout to update the popup's location. For example:

<html>
<head>
        <title>This is the from page</title>
</head>
<body>

    <script type="text/javascript">

        function redirect(destination)
        {
            newWindow = window.open(destination, "_blank", "width=790,height=520");
            newWindow.document.write("<h1>Now redirecting to destination page in 4 seconds...</h1>");
            setTimeout(function(){ newWindow.location=destination;}, 4000);
        }

        redirect("./to.html");
    </script>
</body>
0

精彩评论

暂无评论...
验证码 换一张
取 消