Paul
Paul

Reputation: 11756

Is there an href alternative for no-javascript?

I'm currently using javscript and classes to handle my click events like so:

<a class="forgotten" href="#">Forgot your password?</a>

$('.forgotten').click(function(){
    location.href = "forgotten.php"; 
    return false;
});

Is there a way to evaluate that function first and if javascript is disabled simply go to forgotten.php?

I know I could easily remove the class and add forgotten.php to href but I'm making heavy use of jquery address so it would break some of my site.

Upvotes: 0

Views: 297

Answers (3)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039120

Sure, start by designing your markup so that it works even if the user has javascript disabled:

<a class="forgotten" href="/forgotten.php">Forgot your password?</a>

and then you could use unobtrusive javascript:

$(function() {
    $('.forgotten').click(function(evt) {
        evt.preventDefault();
        window.location.href = "forgotten.php"; 
    });
});

obviously you probably want to do something more fancy in this javascript .click() handler other than simply redirecting to forgotten.php which is what an anchor with disabled javascript already does. You could do for example some progressive enhancement. Like an AJAX call with some animation or something.

Now if the user has javascript disabled he will simply be redirected to forgotten.php. And if he doesn't, well, put your imagination into action.

Upvotes: 6

JaredPar
JaredPar

Reputation: 755141

If you want to deal with a scenario where javascript may be disable then I would take the opposite approach. Start by formatting the HTML as you would if Javascript isn't there. Then use javascript to format it as you would prefer after document load.

Upvotes: 0

Rich Bradshaw
Rich Bradshaw

Reputation: 72985

Using # as the href is bad practice, and isn't how to do things.

If you simply replace the # with forgotten.php, your code will do exactly what you want. The JS will be triggered first.

Upvotes: 0

Related Questions