[Javascript] Passing a variable to a Function
Peter Brunone
peter at brunone.com
Fri Nov 21 17:26:20 CST 2003
These are all valid concerns, but with the exception of 4 --
which shouldn't happen if you're testing your code first -- they seem to
cover a very small percentage of web users/situations (which I guess is
why I've never had any problem using the javascript pseudoprotocol).
In #3, how do non-JS browsers replace "#" (stay on the current
page) with an alternative link?
Thanks for the exposition.
-Peter
-----Original Message-----
From: javascript-bounces at LaTech.edu On Behalf Of liorean
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-protoc
ol.html>
<http://www.w3.org/TR/WCAG/#tech-scripts>
<http://jibbering.com/techniques/>
More information about the Javascript
mailing list