Thursday, April 30, 2015

jQuery and JavaScript Coding: Examples and Best Practices - RULE #1: SEPARATE JAVASCRIPT FUNCTIONALITY

CrossBad markup:
Never include Javascript events as inline attributes. This practice should be completely wiped from your mind.
<a onclick="doSomething()" href="#">Click!</a>
TickGood markup:
All Javascript behaviours should be included in external script files and linked to the document with a <script> tag in the head of the page. So, the anchor tag would appear like this:
<a href="backuplink.html" class="doSomething">Click!</a>
And the Javascript inside the myscript.js file would contain something like this:
...

$('a.doSomething').click(function(){
 // Do something here!
 alert('You did something, woo hoo!');
});
...

No comments:

Post a Comment