Reply to Re: href="javascript:func()" vs href="#" onclick="javascript:func()"

Your name:

Reply:


Posted by Richard Cornford on 01/21/03 11:51

CRON wrote:
> Howdy!
> I was wondering which is better to use:
>
> href="javascript:func()"

The only circumstances under which you should consider using a
javascript: pseudo-protocol HREF is when the expression evaluates as a
string of HTML that it intended define the replaced content of the
current page (or the entire contents of a new window), and mostly not
even then as the result is inevitably javascript dependent.

A significant practical reason for this is that some browsers (and
importantly including windows IE browser) regard the activation of such
a link as navigation. A consequence of this apparent navigation is that
the browser puts the current page into a 'waiting' state in anticipation
of the navigation resulting in the current page being replaced. In this
'waiting' state some resource hungry activity is closed down by the
browser.

The simplest demonstration of this phenomenon is to create a page
containing an animated GIF and a link with a javascript pseudo-protocol
HREF, load it into IE 6, and observe that the animated GIF promptly
stops animating as soon an the HREF is activated.

The list of things that stop working as expected once a javascript
pseudo-protocol HREF has been activated includes META refresh stopping
working, Flash-javascript interaction problems, image swapping and
pre-loading issues and a number of other scripting related issues.
Indeed the consequences of the use of javascript pseudo-protocol HREF
regularly feature in questions asked in the comp.lang.javascript
newsgroup, although no comprehensive list of related issues has been
created because the general conclusion is that such links should never
be used and once their use has ceased no consequential issues remain to
be studied.

> OR
>
> href="#" onclick="javascript:func()"

In javascript syntax the "javascript:" at the beginning of that onclick
attribute's value is a label, used with the - continue - and break -
statements to define the exit points of loops. The rest of the attribute
value contains no loops, continue or break statements, so the label is
redundant.

Microsoft took advantage of this construct being syntactically valid in
javascript to allow this label to be used to define the type of
scripting language that would be used to interpret the value of the
onclick attribute, but they also made JScript the default, so unless an
alternative scripting language is used on a page in a way that stops
JScript being the default the label is still redundant on Microsoft
browsers.

This also means that if a label is wanted in the value of an intrinsic
event attribute 'javascript' is the one label that should never be used
(well, not the 'one' as 'vbscript', or the name of any other installed
scripting language, would also be a bad ideal).

> Problem is the second one, anchors to the top of the
> page which is very messy in most cases.

Only if you don't cancel the default action of the link.

Of the two the second is better because the former should never be used.
The latter would be improved by having an ability to cancel the link,
preferably conditionally.

Ultimately the best option may be to use some other sort of element
(i.e. <input type="button">) to trigger activity that is not navigation.

Richard.

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация