104

I have some mockup in HTML

<a href="javascript:ShowOld(2367,146986,2)"><img title="next page" alt="next page" src="/themes/me/img/arrn.png"></a>

I got the response from server when I sent the request.

With this mockup I got as a response of AJAX request that sends my code to server.

Well, everything is fine but when I click on the link the browser wants to open the function as link; meaning after click I see the address bar as

javascript:ShowOld(2367,146986,2)

means browser thing that's url if I want to do this in firebug that's work. Now I want to do that then when anyone clicks the link then the browser tries to call the function already loaded in the DOM instead of trying to open them in browser.

Brett DeWoody
  • 59,771
  • 29
  • 135
  • 184
  • Similar question is answered in Stackoverflow itself. https://stackoverflow.com/questions/1070760/javascript-function-in-href-vs-onclick/11348403#11348403 – Scaria Sebastian Aug 08 '16 at 05:29

7 Answers7

228

That syntax should work OK, but you can try this alternative.

<a href="javascript:void(0);" onclick="ShowOld(2367,146986,2);">

or

<a href="javascript:ShowOld(2367, 146986, 2);">

UPDATED ANSWER FOR STRING VALUES

If you are passing strings, use single quotes for your function's parameters

<a href="javascript:ShowOld('foo', 146986, 'bar');">
Dutchie432
  • 28,798
  • 20
  • 92
  • 109
12

If you only have as "click event handler", use a <button> instead. A link has a specific semantic meaning.

E.g.:

<button onclick="ShowOld(2367,146986,2)">
    <img title="next page" alt="next page" src="/themes/me/img/arrn.png">
</button>
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • Is there any way to make a button look like a link instead of a button? – Ben Page Feb 15 '11 at 13:17
  • @Ben Page: Yes, with CSS. At least you get a good approximation. See my answer and the comments here: http://stackoverflow.com/questions/4842953/or-javascriptvoid0/4842962#4842962 – Felix Kling Feb 15 '11 at 13:20
7

Try to make your javascript unobtrusive :

  • you should use a real link in href attribute
  • and add a listener on click event to handle ajax
soju
  • 25,111
  • 3
  • 68
  • 70
6

I use a little CSS on a span to make it look like a link like so:

CSS:

.link {
    color:blue;
    text-decoration:underline;
    cursor:pointer;
}

HTML:

<span class="link" onclick="javascript:showWindow('url');">Click Me</span>

JAVASCRIPT:

function showWindow(url) {
    window.open(url, "_blank", "directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes");
}
tolsen64
  • 881
  • 1
  • 9
  • 22
3

<a href="#" onclick="javascript:ShowOld(2367,146986,2)">

shwz
  • 426
  • 1
  • 6
  • 22
nico
  • 83
  • 1
3

Your should also separate the javascript from the HTML.
HTML:

<a href="#" id="function-click"><img title="next page" alt="next page" src="/themes/me/img/arrn.png"></a>

javascript:

myLink = document.getElementById('function-click');
myLink.onclick = ShowOld(2367,146986,2);

Just make sure the last line in the ShowOld function is:

return false;

as this will stop the link from opening in the browser.

hellsgate
  • 5,905
  • 5
  • 32
  • 47
0

href is optional for a elements.

It's completely sufficient to use

<a onclick="ShowOld(2367,146986,2)">link text</a>
Colin 't Hart
  • 7,372
  • 3
  • 28
  • 51