Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call javascript from a href?

How to call javascript from a href?

like:

<a href="<script type='text/javascript'>script code</script>/">Call JavaScript</a> 
like image 593
user2033281 Avatar asked May 02 '13 12:05

user2033281


People also ask

Can we call a JavaScript function on a href?

In JavaScript, you can call a function or snippet of JavaScript code through the HREF tag of a link. This can be useful because it means that the given JavaScript code is going to automatically run for someone clicking on the link. HREF refers to the “HREF” attribute within an A LINK tag (hyperlink in HTML).

What is JavaScript href?

The href attribute specifies the URL of the page the link goes to. If the href attribute is not present, the <a> tag will not be a hyperlink. Tip: You can use href="#top" or href="#" to link to the top of the current page!


2 Answers

<a onClick="yourFunction(); return false;" href="fallback.html">One Way</a>

** Edit **
From the flurry of comments, I'm sharing the resources given/found.

Previous SO Q and A's:

  • Do you ever need to specify 'javascript:' in an onclick? (and the IE related A's following)
  • javascript function in href vs onclick

Interesting reads:

  • http://crisp.tweakblogs.net/blog/the-useless-javascript-pseudo-protocol.html
  • https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/label
like image 104
Dawson Avatar answered Oct 07 '22 23:10

Dawson


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

where the function then has to return false so that the browser doesn't go to another page.

But I'd recommend to use jQuery (with $(...).click(function () {})))

like image 21
bwoebi Avatar answered Oct 07 '22 22:10

bwoebi