tags:

views:

1541

answers:

7

How To Use A Link To Call Javascript?

+1  A: 
<a href="javascript:alert('Hello!');">Clicky</a>
Matt Grande
A: 

I think you can use the onclick event, something like this:

<a onclick="jsFunction();">
David Zaslavsky
A: 
<a onclick="jsfunction" href="#">

or

<a onclick="jsfunction" href="javascript:void(0);">
fluid_chelsea
I'd recommend the second one, as the first one scrolls you to the top of the page.
Matt Grande
Yep it definitely will, unless you add a "return false;" after your function.
fluid_chelsea
Yes, use the 2nd suggestion
TravisO
This is 1998 code. Use one of the unobtrusive solutions. Please.
Andrew Hedges
Use neither one! Links are for linking, they're not dummy elements to call javascript.
I.devries
if your going to put javascript inline do it this way:<a onclick="jsfunction;return false" href="linkasnormal">
Fire Crow
This should not be the answer
Josh Stodola
+4  A: 

Or, if you're using PrototypeJS

<script type="text/javascript>
  Event.observe( $('thelink'), 'click', function(event) {
      //do stuff

      Event.stop(event);
  }
</script>

<a href="#" id="thelink">This is the link</a>
Mark Biek
Unobtrusive, I like it!
Matt Grande
I love not having to put event handling functions in-line.
Mark Biek
It's the only way to go. Please vote down the inline answers. It's a practice for which there is no excuse these days.
Andrew Hedges
@Andrew: Please leave a comment along with your downvote explaining why the inline answers are bad.
Bill the Lizard
+13  A: 

Unobtrusive JavaScript, no library dependency:

<html>
<head>
    <script type="text/javascript">

        // Wait for the page to load first
        window.onload = function() {

          //Get a reference to the link on the page
          // with an id of "mylink"
          var a = document.getElementById("mylink");

          //Set code to run when the link is clicked
          // by assigning a function to "onclick"
          a.onclick = function() {

            // Your code here...

            //If you don't want the link to actually 
            // redirect the browser to another page,
            // "google.com" in our example here, then
            // return false at the end of this block.
            return false;
          }
        }
    </script>
</head>
<body>
    <a ID="mylink" href="http://www.google.com"&gt;linky&lt;/a&gt;        
</body>
</html>
EndangeredMassa
Can you explain why this is better than the currently accepted answer, and where the script should go in the page (because this is pretty clearly a beginner-level question)?
Bill the Lizard
Sure thing. Updated.
EndangeredMassa
+2  A: 

And, why not unobtrusive with jQuery:

  $(function() {
    $("#unobtrusive").click(function(e) {
      e.preventDefault(); // if desired...
      // other methods to call...
    });
  });

HTML

<a id="unobtrusive" href="http://jquery.com"&gt;jquery&lt;/a&gt;
Mister Lucky
+1  A: 

Unobtrusive Javascript has many many advantages, here are the steps it takes and why it's good to use.

  1. the link loads as normal:

    <a id="DaLink" href="http://host/toAnewPage.html"&gt;click here</a>

this is important becuase it will work for browsers with javascript not enabled, or if there is an error in the javascript code that doesn't work.

  1. javascript runs on page load:

     window.onload = function(){
            document.getElementById("DaLink").onclick = function(){
                   if(funcitonToCall()){
                       // most important step in this whole process
                       return false;
                   }
            }
     }
    
  2. if the javascript runs successfully, maybe loading the content in the current page with javascript, the return false cancels the link firing. in other words putting return false has the effect of disabling the link if the javascript ran successfully. While allowing it to run if the javascript does not, making a nice backup so your content gets displayed either way, for search engines and if your code breaks, or is viewed on an non-javascript system.

best book on the subject is "Dom Scription" by Jeremy Keith

Fire Crow