11

In Javascript I can redirect and preserve the query string and fragment ID like this:

window.location = "NEW_LOCATION" + window.location.search + window.location.hash;

For sites without Javascript you can use the http-equiv meta header. But this drops the query string and fragment ID:

<head>
    <meta http-equiv="Refresh" content="300; url=NEW_LOCATION" />
</head>

Is there a way to do the equivalent using http-equiv="refresh" that preserves the query string and fragment ID?

kanaka
  • 70,845
  • 23
  • 144
  • 140

3 Answers3

10

You could use JavaScript to update the meta tag with the query string and hash.

Update A better approach for IE8+ would be a noscript tag and a JavaScript powered redirect. Add the redirect as data-destination attribute on the html element so the script can grab it easily.

<!DOCTYPE html>
<html data-destination="http://stackoverflow.com">
  <head>
    <noscript><meta id="redirect" http-equiv="refresh" content="0; url=http://stackoverflow.com"></noscript>
  </head>

  <body>
    This page has moved. Redirecting...
    
  <!-- Redirect in JavaScript with meta refresh fallback above in noscript -->
  <script>
  var destination = document.documentElement.getAttribute('data-destination');
  window.location.href = destination + (window.location.search || '') + (window.location.hash || '');
  </script>
  </body>
</html>
jwal
  • 7,290
  • 1
  • 21
  • 11
2

Not without a server-side scripting language which puts the proper url in the HTML tag (or sends a Refresh header directly).

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
0

I am using the following Script (after the Form, but before end of body). Required URL ("hidAutoURL") is stored in a hidden variable first (from the server side).

document.write is used to update the meta.

<BODY>

    <FORM>
    </FORM>

    <script>
        function getAutoURL()
        {
            var url = document.getElementById('hidAutoURL').value;
            return url;
        }

        //Update META for auto-refresh
        var configuredTime = document.getElementById('hidRefrTime').value;
        var content = configuredTime + ';url=' + getAutoURL('url');
        document.write('<meta http-equiv="refresh" content="'+content + '"/>');

    </script>

</BODY>

References

  1. What is the correct way to write HTML using Javascript?
  2. Changing the content of meta refresh does not change refreshing time
  3. Using document.write
Community
  • 1
  • 1
LCJ
  • 22,196
  • 67
  • 260
  • 418