JD Isaacks
JD Isaacks

Reputation: 57974

Calling JS functions from links in IE?

If I have a function like this:

function foo()
{
    //...
    return false;
}

I can call it like this:

<a href="#" onClick="foo()">Run Foo</a>

However, in all browsers, this puts an # in the URL which I do not want.

So instead I do this:

<a href="javascript:foo()">Run Foo</a>

Which works fine in chrome but in IE it loads a page containing the string false.

Whats the best practice here?

Upvotes: 1

Views: 80

Answers (2)

Richard JP Le Guen
Richard JP Le Guen

Reputation: 28753

Like @wrumbsy says...

You don't need the javascript: protocol.

<a href="#" onclick="foo(); return false">Run Foo</a>

... but this means you don't need an anchor <a> either. Only use anchors for hyperlinks; not for JS-enhanced interactivity.

A span will work just as well, with cursor:pointer; CSS property:

<span style="cursor:pointer;" onclick="foo(); return false">Run Foo</span>

Upvotes: 1

Walter Rumsby
Walter Rumsby

Reputation: 7535

You don't need the javascript: protocol.

<a href="#" onclick="foo(); return false">Run Foo</a>

is all you need.

Upvotes: 3

Related Questions