110

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.

1

7 Answers 7

243

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');">
Sign up to request clarification or add additional context in comments.

3 Comments

How to pass a string escaping double quotes in the arguments? H=The example has only integers.
@jeffrycopps try single quotes inside your function
@ineedme Answer Updated with that information. Thanks.
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>

2 Comments

Is there any way to make a button look like a link instead of a button?
@Ben Page: Yes, with CSS. At least you get a good approximation. See my answer and the comments here: stackoverflow.com/questions/4842953/or-javascriptvoid0/…
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

2 Comments

href is optional, just omit it.
An a tag is an anchor; it doesn’t have to have a link.
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");
}

3 Comments

The above 'url' function parameter gives me string "url" instead of actual url
You're supposed to put the actual URL in the showWindow function. For example: javascript:showWindow('stackoverflow.com');
I cannot hard code that as I am creating it using dom and I have no control over the url
5

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

3 Comments

Not the best solution as this will jump to the top of the page.
For href instead of # use href="javascript:void(0)
href is optional, just omit it.
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.

1 Comment

href is optional, just omit it.
0

href is optional for a elements.

It's completely sufficient to use

<a onclick="ShowOld(2367,146986,2)">link text</a>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.