My Location

Apr 15, 2009

Firing script with an anchor

Quite often I want an anchor (link) to execute a script, and usually I fall into the trap of trying to use onclick and set href to an empty string or leave it out entirely. However, href is a required attribute and empty string is a valid value which means "link to self", so the correct solution is to put the script into the href attribute, prefixed by "javascript:".

For example, here is an anchor which does the same thing as pressing the browser's Back button:

<a href="javascript:history.back();">Back</a>

Thanks JWhittaker for setting me straight.

1 comment:

  1. Brett, we're using Contivity VPN and attempting to get an iPhone and iPad to connect - pptp, l2pt - but running into problems. It works for local CES account (no encryption), and Active Director (CES -> Radius -> AD) without encryption, but no luck with SecurID (CES -> Radius -> RSA SecurID). Have you successfully connected 1) using a local account and/or AD account with encryption, and 2) using SecurID, either softtoken or keyfob? If yes, I'd sure like to know your secret. Thanks, Gary

    ReplyDelete