javascripthtmlmeta-tagsdata-uri

How to provide base64 encoded data URL in a meta tag


Consider the following HTML:

<html>
    <body>
        <p>Paragraph</p>
        <meta id="meta" http-equiv="refresh">
    </body>
    <script type="text/javascript">  document.getElementById("meta").content="1;url=data:text/html;base64,PFNDUklQVD5hbGVydCgiUHduZWQiKTs8L1NDUklQVD4=";
    </script>
</html>

If you type the data:text/html;base64,PFNDUklQVD5hbGVydCgiUHduZWQiKTs8L1NDUklQVD4= in the Chrome URL address bar, you will get an alert.

But it doesn't happen when you use it in the meta tag above. It shows some error -

Not allowed to navigate top frame to data URL: data:text/html;base64,PFNDUklQVD5hbGVydCgiUHduZWQiKTs8L1NDUklQVD4=

Mainly I want to auto redirect to a different page. But using the base64 encoded URL. If I do <meta http-equiv="refresh" content="1;url=https://www.google.com">, the page does gets redirected to google.com after a second.

But when I change the same to content="1;data:text/html;base64,aHR0cHM6Ly93d3cuZ29vZ2xlLmNvbQ==", I get the same error:

Not allowed to navigate top frame to data URL

Even if you paste the string: data:text/html;base64,aHR0cHM6Ly93d3cuZ29vZ2xlLmNvbQ== in the browser's address bar it will show you http://www.google.com

I want to do it inside the HTML using JavaScript. Is there any way to achieve this?


Solution

  • how about using a redirect in javascript like explained here: https://www.w3schools.com/howto/howto_js_redirect_webpage.asp within an onload() function

    <html>
    <head>
      <!-- ... -->
      <script>
      function myRedirectFunction() {
        // option 1 
        // window.location.href = "http://www.w3schools.com";
        // option 2
        window.location.replace("http://www.w3schools.com");
      }
      </script>
    </head>
    <body onload="myRedirectFunction()">
       <!-- ... -->
    </body>
    </html>