0

I'm really new to js and just need a short script that redirects users to an URL from another website. This is my script so far:

<!DOCTYPE HTML>
<html>
<head><META CHARSET='UTF-8'><title>My webpage</title>
</head>
<body onload="redirectUser();">
    <script>
        function redirectUser() {
            window.location = "https://www.google.com";
        }
    </script>
</body>
</html>

The major issue is that some web browsers (i.e. IE) do not automatically run these scripts (they are prompt to manually activate javascript and activeX in order to run the scripts). If this turns out to be the case, is there a way to automatically display a text in the middle of the screen to warn the user about this or to simply display the link?

Lelio Faieta
  • 6,457
  • 7
  • 40
  • 74
mat
  • 2,412
  • 5
  • 31
  • 69

2 Answers2

2

you can place your link into a <noscript></noscript> tag.

<noscript><a href="https://www.google.com">Go!</a></noscript>
Founded1898
  • 977
  • 5
  • 13
1

You can take advantage of the fact a user doesn't have JavaScript enabled to tell them that they don't have JavaScript enabled.

You can add a div with a message warning about JavaScript being required to use your site, then immediately remove/hide this div with JavaScript.

Users that have JavaScript enabled won't see it as it will have been removed by JavaScript.

Those that don't have JavaScript enabled see the message.

Example below, either disable JS or comment out to see the message.

document.getElementById("jswarning").remove();
#jswarning {
  width: 100%;
  padding: 10px;
  background: red;
  color: #fff;
  text-align: center;
}
header {
  text-align: center;
  background: #333;
  color: #fff;
  font-size:40px;
}
<div id="jswarning">This website requires JavaScript.</div>
<header>Your Website</header>

No script solution (credit to @Founded1898):

<noscript>
    <div id="jswarning">This website requires JavaScript.</div>
</noscript>
<header>Your Website</header>
Brad
  • 8,044
  • 10
  • 39
  • 50
  • This sounds like a nice alternative. Could you please write a small example of the code? That would help me a lot. – mat Feb 28 '17 at 17:02
  • Just place your warning div into a noscript tag. Users with js enabled won't see this message. So you have the same effect with less effort.. – Founded1898 Feb 28 '17 at 17:16
  • Though having just looked up the answer provided by @founded1898, I think his solution is better. – Brad Feb 28 '17 at 17:16