views:

51

answers:

2

I need if JS is enabled then this link will be this

<a href="#" class="collops">see sitemap</a>

And is js is disabled then link should be this

<a href="http://stackoverflow.com" class="collops">see sitemap</a>
+1  A: 

Use a function that runs when your page loads to re-write all of your links (or all selected links). That way page renders with proper anchor tags if JS is off, and if it's on they all get replaced to what you need them to be.

Something along the lines of:

$(document).ready(function(){
    $(".collops").attr("href", "#");
});
R0MANARMY
you mean i should keep this as default in html `<a href="http://stackoverflow.com" class="collops">see sitemap</a>`
metal-gear-solid
@metal-gear-solid: That's how I would do it, yes. Mind you JS may be enabled but "turned off" using something like No Script in FF.
R0MANARMY
@R0MANARMY - and how to do if i want to change link text also
metal-gear-solid
@metal-gear-solid: Looking at the documentation, it doesn't look like `attr` is chainable so you'll have to change it to a `each` call and set the attribute and text that way. You can use the `text` function (http://api.jquery.com/text/#text2) to set the text.
R0MANARMY
+3  A: 

You do not need to do such things. Just have the onclick function return false, like this:

<a href="http://stackoverflow.com/" onclick="redirect(this.href); return false;">link</a>
Residuum
Presumably if JS is enabled he's attaching events to those anchors to do stuff.
R0MANARMY
@R0MANARMY - you are right
metal-gear-solid
Yes, so just `return false` from the event handler attached to the element.
bobince
@bobince: I think that might break if you "disable" JS using the No Script FF plugin. The only example I have is if you go to anandtech (like here: http://www.anandtech.com/show/3732/overclocked-our-custom-radeon-hd-5870-roundup), the dropdown at the bottom of the page still triggers even though No Script supposedly disabled JS. In your case I think `return false` will still fire and prevent proper navigation.
R0MANARMY
If you are assigning the handler that returns false *from JavaScript itself*, then it won't get assigned when JS is disabled (whether by browser setting or by NoScript), so it'll continue to work as a normal link. That's the point of ‘progressive enhancement’.
bobince
Google is using this technique for too long :)
Mihai Iorga