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

David T. Lovering dlovering at gazos.com
Wed Mar 26 06:42:00 CST 2003


Just a notion, but if you want to group more than one code element in a javascript declaration, you can usually get away with a construct like this:

  <a href="javascript:(doSomething(); doSomethingElse(); doAThirdThing(); return false)" ...

I haven't tried it in all possible permutations of JavaScript1.X/JavaScript2.X/JavaScript3.X/JScript, but it works often enough to have proven reliable.

The other way is to build a wrapper routine which does all the 'foreign' bits (including the return false), and then merely invoke it from the href anchor:

  <a href="javascript:doMyStuff()" ...

  <script language="JavaScript">
  <!--
    function doMyStuff() {
      doSomething();
      doSomethingElse();
      doAThirdThing();
      return false;
    }
  // -->
  </script>

  Obviously you can insert parameters in these calls as well, but be forewarned -- global variables don't penetrate wrappers very well inside a "javascript:" handler or anchor reference.  Declare everything, and pass everything.

-- Dave Lovering



there was something I wanted to ask about this though. obviously you can put 2 actions into an onclick like so:
 
<a href="#" onClick="doSomething();return (false);">Link 2</a>
 
but how do you do 2 things using this href="javascript... way:
 
<a href="javascript:doSomething();...
 
and add the return false in the same href?
 
thanks
 
ant
 
 
 
 

-----Original Message-----
From: Jaime Iniesta [mailto:jaime at alazan.com]
Sent: 25 March 2003 09:26
To: javascript at LaTech.edu
Subject: [Javascript] Link to Javascript functions - what is best?



Hello, I've got a question...

 

There are 2 ways to create a link to a JavaScript function. Let's say I've got a function like this:

 

Function doSomething()

{

                        ...

}

 

And I want to link to it later on the HTML, so when the user clicks on the link, the function is executed.

 

The first way to do this is this:

                                    

<a href="javascript:doSomething()">Link 1</a>

 

It works, but I've detected that if the page is still loading, when I click on the link, the page stops loading, and the animated GIFs also stop its animation...

 

So the second way, and I think is the best one, is this:

 

<a href="#" onClick="doSomething()">Link 2</a>

 

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.

 

I think this is the correct way to do this, but I've heard that on some browsers it doesn't work...

 

An example of this second way of opening windows is on this photographs page:

 

http://www.thismustbetheplace.net/talking-heads-pictures-band.asp

 

Thanks,

 

Jaime




Anthony Webster wrote:
> 
>    Part 1.1.1    Type: Plain Text (text/plain)
>              Encoding: quoted-printable
> 
>    Part 1.2    Type: Plain Text (text/plain)
>            Encoding: 7bit


More information about the Javascript mailing list