Which of the following ways is a better way to call a js function from an a tag?
<a href="javascript:someFunction()">LINK</a>
OR
<a href="#" onclick="someFunction();" return false;">LINK</a>
I have seen this question here, but it says <span onclick="someFunction()">
is a better option. But due to some reasons I have to use <a>
links.
EDIT: I am looking for a cross browser & cross platform solution which should work on androids & iPads too.
Calling a function using external JavaScript file Js) extension. Once the JavaScript file is created, we need to create a simple HTML document. To include our JavaScript file in the HTML document, we have to use the script tag <script type = "text/javascript" src = "function.
The first method is to call the JavaScript function in HTML. For this, you have to create a function then define this function either in the head section or body section of the HTML document. You can either create a link or a button and then an onclick() event is associated with them in order to call this function.
Calling a Function through call() and apply() While working with JavaScript functions you've to keep in mind that the functions can have their own properties and methods and call() and apply() being the two of such methods.
Neither is good.
Behaviour should be configured independent of the actual markup. For instance, in jQuery you might do something like
$('#the-element').click(function () { /* perform action here */ });
in a separate <script>
block.
The advantage of this is that it
Furthermore, it degrades gracefully (but so would using the onclick
event) since you can provide the link tags with a href
in case the user doesn’t have JavaScript enabled.
Of course, these arguments still count if you’re not using jQuery or another JavaScript library (but why do that?).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With