views:

170

answers:

3

Can we use 2 different url on same for javascript disabled and enabled condition

If javascript is enabled then link should be <a href="link1">

and If javascript is disabled then link should be <a href="link2">

+2  A: 

Yes, just write a JavaScript which finds the element you want to change the href of when the page has loaded.

Example using JQuery:

$(function () {
    $('a.changeable').each(function () {
        $(this).attr('href', 'http://google.com/lol');
    });
});

(Untested, but you get the idea.)

So basically, if the user has disabled JavaScript, the default link is used, otherwise the new URL given by the script is used. So your HTML should contain the link that should be used when JavaScript is disabled.

Deniz Dogan
A: 

If I understand you correctly, you can have one url on the html page, and if javascript is working, have the javascript change the url on the anchor.

That way you can ensure that the second url only works if javascript is enabled.

The problem is that if javascript was enabled when the page was loaded, and later disabled then you won't know.

But, if you just have an onclick on the anchor, then you can change the url when it is clicked on, then it will always work correctly, as, if they disable javascript after the page is loaded it will still go to the non-javascript url, and if the onclick works then it will go to the other url.

James Black
+5  A: 

You could set the JS disabled URL in the markup, then on page load use JS to replace the url with the enabled URL.

HTML:
<a id="id" href="js_disabled_url" />Link</a>


jQuery example:
$(function() {
    $('#id').attr('href','js_enabled_url');
});

This should degrade gracefully.

Neil Aitken
thanks it is useful for me because I'm already using jquery.
metal-gear-solid
You're welcome, The question was tagged with jQuery so I made the assumption.
Neil Aitken