Displaying something if javascript is enabled and compatible

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Displaying something if javascript is enabled and compatible

Post 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...
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

you could use the write() method and just write out the link on the page.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

thanks... that's not much different than the method I posted though.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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.
quentin.lindsey
Forum Newbie
Posts: 1
Joined: Mon Nov 06, 2006 3:48 pm

Post 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>
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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...)
Post Reply