[Javascript] Link to Javascript functions - what is best?

Mike Ledig hobbes_tiger at gmx.de
Tue Mar 25 03:49:12 CST 2003


Hello Jaime,


Jaime Iniesta schrieb:
> Hello, I?ve got a question?
> 
> Function doSomething()
> 
> {
> 
>                         ?
> 
> }

Caution: function must start with a smal-caps "f" ;-)


> That is, having a null link (a link to #) and calling the function on 
> the onClick event of the link. In this way, the loading of the page is 
> not stopped.
> 

Well, the problem with the second Version is, that most browsers 
"navigate" to "#" - as it is a named anchor in HTML. So, most browsers 
go to the top of the page - or some browsers go to the bottom of the page.

I always use the following:
a href="alternat.htm" onclick="doSomething(); return false;"

- the onclick attribute should be written in small-caps (some Browsers 
i.e. XHTML compatible browsers don't allow the event attributes written 
onXYZ)

- in the onlick attribute I first call the function doSomething() and 
then give the browser the command "return false;". With this command, 
the browsers doesn't navigate to the given href attribute (the browser 
ignores the href!!)

- with the href attribute, I can use a alternativ HTML page, because, if 
the users browser disabled javascript, nothing would happen, when the 
link is clicked. But, with javascript disabled, the browser doesn't get 
the "reutn false" command, and the browser uses the href attribute.
In some cases it's not necessary to place a alternative HTML page, then 
the href is "#"

HTH and greetings from sunny Munich
Mike.

-- 
Training und Projekte
http://www.gidel.de



More information about the Javascript mailing list