Page 1 of 1

Displaying something if javascript is enabled and compatible

Posted: Fri Oct 27, 2006 6:24 pm
by Luke
I realize that javascript can detect if it's enabled simply by attempting to execute itself, and then you can supply a user who has no javascript available with content via <noscript> but I've always found it frustrating. Is there a better way to detect if the user has javascript enabled?

Code: Select all

var element = document.getElementById('element');
element.innerHTML = '<a href="javascript;" onclick="someFunction()">Click Here!</div>';

Code: Select all

<div id='element'>
<noscript><a href="you_are_a_loser.html">Click Here!</noscript>
</div>
The reason I am asking is that I want to have a link that if javascript is enabled, it executes a javascript function, otherwise, it goes to a page I specify...

Posted: Fri Oct 27, 2006 6:36 pm
by Burrito
you could use the write() method and just write out the link on the page.

Posted: Fri Oct 27, 2006 9:18 pm
by Luke
thanks... that's not much different than the method I posted though.

Posted: Fri Oct 27, 2006 9:29 pm
by Burrito
then I guess I'm confused as to what you're trying to accomplish? Do you want to automatically redirect if they don't have js enabled? If so, you could write out a meta refresh I suppose.

Posted: Mon Nov 06, 2006 3:59 pm
by quentin.lindsey
You can add the anchor to your page with a specific id and the href set to the link you want if the user does not have javascript enabled and then in your window.onload method change the href via javascript. Here is a quick example:

Code: Select all

<html>

<head>
  <script type="text/javascript">
    window.onload = function() {
      with (document.getElementById("anchorid")) {
        href = "javascript:";
        onclick = function() { YourFunction(); };
      }
    }
  </script>
</head>

<body>
  <div><a id="anchorid" href="yournoscriptlinkhere" /></div>
</body>

</html>

Posted: Mon Nov 06, 2006 4:08 pm
by Luke
that sounds much closer to what I was looking for... thanks! I'll try that. (not that your advice wasn't appreciated burrito...)