[Javascript] Passing a variable to a Function

liorean liorean at f2o.org
Thu Nov 20 14:56:00 CST 2003


Peter Brunone wrote:
>>However, I would recommend you to use the onclick event instead, be sure 
>>to return false after you call the function, and use # as the target for 
>>the href attribute.
>    Why do you recommend doing that?  It's never made sense to me to create an extra handler when there's already a default handler that suffices.  I've heard several folks over the years who agree with you, but none of them have given any further explanation.

Because of a number of things:

1. The validity reasons:
   'javascript' isn't a valid URI protocol. It behaves like one, though, 
that's why it is often called a pseudoprotocol. It is actually like the 
'view-source', 'res', 'chrome'  and 'about' protocols a proprietary 
browser protocol - it just happens to be supported on all browsers with 
JavaScript support, and behave in (roughly) the same way. Another such 
protocol, 'mocha' (presumably Mocha was a suggested name for then being 
LiveScript, but they settled on JavaScript), was present in nn2 but is 
long since forgotten now.

2. The backward compatibility reasons:
   Old non JavaScript savvy browsers display the script (kinda like the 
'about' pseudoprotocol does) instead of run it, replacing the current page.

3. The accessibility reasons:
   You are creating a link that JavaScript disabled browsers can not 
follow, without giving them an alternative. It's better for the browsers 
that can't follow it to replace it by an alternative link, and override 
that link with the JavaScript only for those browsers that can run it.

4. The 'embarrasment' reason:
   If you do not ensure that the script returns undefined, it's return 
value will replace the current page. This may come back and bite you if 
you change your pages without testing first, for instance.


Some material:
<http://www.gazingus.org/html/Proper_Use_of_the_javascript_Pseudo-protocol.html>
<http://www.w3.org/TR/WCAG/#tech-scripts>
<http://jibbering.com/techniques/>

-- 
liorean <mailto:liorean at user.bip.net>

ViewStyles, ViewScripts, ToggleStyles and GraphicsInfo bookmarklets and 
Theme Switcher, Cookies Handler scripts:
<http://liorean.web-graphics.com/>




More information about the Javascript mailing list