Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

FAQ Topic - I have <a href="javascript:somefunction()"> what ... ? (2012-10-26)

5 views
Skip to first unread message

FAQ server

unread,
Oct 25, 2012, 7:00:02 PM10/25/12
to
-----------------------------------------------------------------------
FAQ Topic - I have <a href="javascript:somefunction()"> what
... ?
-----------------------------------------------------------------------

Whatever the rest of your question, this is generally a very bad idea.
The `javascript:` pseudo protocol was designed to replace the
current document with the value that is returned from the expression.
For example:

<a href="javascript:'&lt;h1&gt;' + document.lastModified + '&lt;/h1&gt;'">lastModified</a>

will result in replacing the current document with the value
returned from `document.lastModified`, wrapped in an `<h1>`
tag.

When the expression used evaluates to an `undefined` value
(as some function calls do), the contents of the current page are not
replaced. Regardless, some browsers (notably IE6) interpret this as
navigation and will enter into a 'navigation' state where GIF
animations and plugins (such as movies) will stop and navigational
features such as `META` refresh, assignment to `location.href`, and image
swaps fail.

It is also possible for IE to be configured such that it supports
javascript but not the `javascript:` protocol. This results
in the user seeing a protocol error for `javascript:` URIs.

The `javascript:` pseudo protocol creates accessibility and
usability problems. It provides no fallback for when the script is not
supported.

Instead, use
`<a href="something.html" onclick="somefunction();return false">`
where `something.html` is a meaningful alternative. Alternatively,
attach the `click` callback using an event registry.

<URL: http://jibbering.com/faq/example/jsuri/>
Set/Navigate to a Location [ref 1]
Top Ten Web-Design Mistakes of 2002 [ref 2]

References:
-----------

[1] http://groups.google.com/group/comp.lang.javascript/msg/f665cfca3b619692
[2] http://www.useit.com/alertbox/20021223.html


The complete comp.lang.javascript FAQ is at
http://jibbering.com/faq/

--

The sendings of these daily posts are proficiently hosted
by http://www.pair.com.

0 new messages