2

I'm curious whats the best way to call a JS function with a href link in HTML. I don't use a library and i see alot of mention about jquery using event handlers ...

But if im not using a library can it still be done or will i have to use an on click type call ?

4
  • Just jump on the jQuery bandwagon, and you'll thank me for the rest of your life. I was originally inspired by SO hazing :-D Commented Mar 27, 2012 at 3:21
  • Thats lazy man's talk :P Commented Mar 27, 2012 at 3:28
  • 1
    I'm one of those people that like to do everything myself, but honestly once you try jQuery, you'll never go back. There's literally a billion situations where jQuery has a function which would encapsulate the 3000 lines of codes you'd need to write yourself. Do it..... Commented Mar 27, 2012 at 3:38
  • jQuery: cigarettes for web developers. I take that back. Cigarettes are cigarettes for web developers Commented Mar 27, 2012 at 3:53

3 Answers 3

4

You can use event handlers with plain javascript. No framework is required. Here's a cross browser function I use:

// add event cross browser function addEvent(elem, event, fn) { if (elem.addEventListener) { elem.addEventListener(event, fn, false); } else { elem.attachEvent("on" + event, function() { // set the this pointer same as addEventListener when fn is called return(fn.call(elem, window.event)); }); } } 

And, an example of using it would be like this:

HTML:

<a id="myLink" href="#">Click ME</a> 

Javascript:

var link = document.getElementById("myLink"). addEvent(link, "click", function(e) { // process the click on the link here }); 

If you don't want the default click of a link to happen, then you need to prevent the default behavior from the event handler like this:

var link = document.getElementById("myLink"). addEvent(link, "click", function(e) { // process the click on the link here // prevent default action of the click if (e.preventDefault) { e.preventDefault(); // normal browsers } else { e.returnValue = false; // older versions of IE (yuck) } }); 
Sign up to request clarification or add additional context in comments.

7 Comments

I'll try it and see if i can get it to work! How do you structure a href for such a function ?
So then i do <a href="#" id="myLink">Click ME</a> ?
@Dave - I've added further examples to my answer.
Ah thanks, though you have slightly lost me with the last part of the script regarding the "default click of a link" ? What did you mean by this ?
@Dave - when a user clicks on a link, if the default behavior is not blocked, the browser will process the href and attempt to go to it. Even if the href="#", it can cause the page scroll to change. To tell the browser that you've handled the click and you do not want it to do anything further with that click, you have to prevent the default behavior with the code I've shown.
|
2

try this

function test() { alert (''); } <a href="#" onclick="test();" />

Comments

2

Basically there are two ways:

<a href="javascript:someFunction(...)">...</a> 

and

<a href="#" onclick="return someFunction(...)">...</a> 

(in this case someFunction must return false)

I prefer the latter.

2 Comments

Is there much difference in terms of why some one would chose one over the other ?
In the latter case browsers don't show "javascript:function" in the bottom status bar on hovering mouse over (which is ugly, IMO).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.