Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > HTML > href="javascript:func()" vs href="#" onclick="javascript:func()"

Reply
Thread Tools

href="javascript:func()" vs href="#" onclick="javascript:func()"

 
 
CRON
Guest
Posts: n/a
 
      06-18-2006
Howdy!
I was wondering which is better to use:

href="javascript:func()"

OR

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

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

Thanks
Ciaran ;c)

 
Reply With Quote
 
 
 
 
Jonathan N. Little
Guest
Posts: n/a
 
      06-18-2006
CRON wrote:
> Howdy!
> I was wondering which is better to use:
>
> href="javascript:func()"
>
> OR
>
> href="#" onclick="javascript:func()"
>
> Problem is the second one, anchors to the top of the page which is very
> messy in most cases.
>


Better to have

<a href="someRealUrl" onclick="return someJavaScriptFunction()">...

That way if someone has JavaScript disabled they are not left wondering
why the link goes nowhere!

--
Take care,

Jonathan
-------------------
LITTLE WORKS STUDIO
http://www.LittleWorksStudio.com
 
Reply With Quote
 
 
 
 
CRON
Guest
Posts: n/a
 
      06-18-2006
Cheers again Jonathan,
But what if you don't want the link to go anywhere? If it is solely
there to run the javascript function? Is there anything wrong with
href="javascript:func()" ? It is simple and clean and makes more sense
to me.

Ciaran


Jonathan N. Little wrote:

> CRON wrote:
> > Howdy!
> > I was wondering which is better to use:
> >
> > href="javascript:func()"
> >
> > OR
> >
> > href="#" onclick="javascript:func()"
> >
> > Problem is the second one, anchors to the top of the page which is very
> > messy in most cases.
> >

>
> Better to have
>
> <a href="someRealUrl" onclick="return someJavaScriptFunction()">...
>
> That way if someone has JavaScript disabled they are not left wondering
> why the link goes nowhere!
>
> --
> Take care,
>
> Jonathan
> -------------------
> LITTLE WORKS STUDIO
> http://www.LittleWorksStudio.com


 
Reply With Quote
 
Dan
Guest
Posts: n/a
 
      06-18-2006

CRON wrote:
> Cheers again Jonathan,
> But what if you don't want the link to go anywhere? If it is solely
> there to run the javascript function? Is there anything wrong with
> href="javascript:func()" ? It is simple and clean and makes more sense
> to me.

[top-posting snipped -- http://mailformat.dan.info/quoting/ ]

In many (most?) cases, the JavaScript link is there to bring up some
content in some fancy way (like in a popup box), so it would make sense
to have a valid link URL as a graceful degradation in the case of
non-JavaScript-enabled users.

Your function can end with "return false;" in order to suppress the
normal link, so it won't do something annoying like jump around in the
page when JavaScript is enabled.

--
Dan

 
Reply With Quote
 
CRON
Guest
Posts: n/a
 
      06-18-2006
That's a good way to deal with it - Never thought of that before.
Thanks a bunch Dan,
Ciaran

 
Reply With Quote
 
CRON
Guest
Posts: n/a
 
      06-18-2006

Best solution found::::

<a href="#" onclick="func();return false">

This is a handy way to stop the page from jumping around but still run
the function.

 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      06-18-2006
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.


 
Reply With Quote
 
Neredbojias
Guest
Posts: n/a
 
      06-18-2006
To further the education of mankind, "CRON" <>
vouchsafed:

>
> Best solution found::::
>
> <a href="#" onclick="func();return false">
>
> This is a handy way to stop the page from jumping around but still run
> the function.


FYI, another best way is:

<a href="javascript:void(0)" onclick="func()">

--
Neredbojias
Infinity has its limits.
 
Reply With Quote
 
Jukka K. Korpela
Guest
Posts: n/a
 
      06-18-2006
Neredbojias <http://www.neredbojias.com/fliam.php?cat=alt.html> scripsit:

>> Best solution found::::
>>
>> <a href="#" onclick="func();return false">
>>
>> This is a handy way to stop the page from jumping around but still
>> run the function.

>
> FYI, another best way is:
>
> <a href="javascript:void(0)" onclick="func()">


The first "best solution" is a link to the start of the page, with a
scripted that overrides the link functionality with the invocation of a
function, when scripting is enabled. That does not sound quite logical to
me. A self-referencing link would be slightly more logical as well as much
more practical when scripting is disabled:
<a name="foo42" href="#foo42" onclick="func();return false">
Of course, if the sole purpose of the "link" is the scripted event, then the
link should be dynamically generated using the scripting language, so that
the link is there if and only if scripting is enabled. In that case, you
could use href="error.html" where error.html is a page that explains that an
unexpected error has occurred, etc.

Using href="javascript:void(0)" does not conform to any published
specification, since there is no such spec that defines javascript: URLs.
Besides, it fails even on some old browsers that do not recognize such URLs
(or pseudo-URLs), despite supporting JavaScript.

--
Jukka K. Korpela ("Yucca")
http://www.cs.tut.fi/~jkorpela/

 
Reply With Quote
 
Adrienne Boswell
Guest
Posts: n/a
 
      06-18-2006
Gazing into my crystal ball I observed "Jukka K. Korpela"
<> writing in
news:hc6lg.5463$:

> Using href="javascript:void(0)" does not conform to any published
> specification, since there is no such spec that defines javascript:
> URLs. Besides, it fails even on some old browsers that do not
> recognize such URLs (or pseudo-URLs), despite supporting JavaScript.
>
>


Yes, and it even fails on some modern browsers, like Opera. AFAIK, Opera
doesn't like href="javascript:anything". Opera is my default browser, and
I'm redoing a site that has tons of links like this - none of 'em work - so
I am replacing them with plain hrefs.

--
Adrienne Boswell at Home
Arbpen Web Site Design Services
http://www.cavalcade-of-coding.info
Please respond to the group so others can share

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57