Re: [TECHS] Use of anchor tags to invoke JavaScript

Matt May wrote:
> 
> Becky_Gibson@notesdev.ibm.com wrote:
>> I'd like to start a thread to discuss the pros and cons of 
>> JavaScript URIs and calling JavaScript functions from an anchor 
>> tag.

[...]


> The method I prefer is unobtrusive JavaScript:
> 
> <a id="foo" href="serversidecode">Action Link</a>
> 
> where "serversidecode" executes the same functionality as the script.
>  Then, within a <script> block (or in a linked .js file):
> 
> function do_my_bidding(obj) { ... } 
> document.getElementById("foo").onclick = do_my_bidding;

I do agree that unobtrusive javascript is the way to go. If the link has
no relevance in a non-JavaScript environment, then it should not be in
the markup. So I'd recommend adding in the a element using DOM methods:

var someNode = document.getElementById('someNode');
var aLink = document.createElement('a');
aLink.onclick = do_my_bidding;
aLink.setAttribute('href', '#javascript');
someNode.appendChild(aLink);


> This allows a fully functional scriptless site to be enhanced with as
>  much JavaScript as an author would like, or at the bare minimum 
> would let authors send users to a failure page to tell them that 
> their lack of script means they can't use the site.

Since JavaScript is available, I wouldn't suggest creating a link
dynamically that falls back to changing the url. So in Matt's example
above, I wouldn't think its good usability to be taken to another page
in the instance of a JavaScript dependent link.

My suggestion instead is that, since the link itself is created using
JavaScript, also create a short message with an internal anchor of
id="javascript", so that when the above created link fails, you don't
get taken to another page, but to a message on the current page that
says something along the lines of "this particular function requires
JavaScript".

This particular message only appears if when the page has loaded
JavaScript is enabled and working, and that the user has turned off
JavaScript after the unobtrusive JavaScript initialisation has been run.


The bottom line is that a non-JavaScript capable browser should never 
see a link that requires JavaScript.



Mike

Received on Wednesday, 17 August 2005 16:18:28 UTC