If I want a function to be executed, I prefer doing inline js:
Click me
becaus
There are a lot of reasons to avoid inline JavaScript and one of the perhaps most important is code maintainability.
A quick example (I am using jQuery simply for demonstration purposes).
Click me
Click me
Click me
Click me
Click me
Click me
What if suddenly you get a request to change all your paragraphs to execute another function? In your example you would have to change everything manually in your HTML code. However if you choose to separate HTML from JavaScript you could simply do it like this.
Click me
Click me
Click me
Click me
Click me
Click me
$('.element').bind('click', doSomethingElse);
The HTML code is also cleaner which allows the designers to focus exclusively on design without fear that they might actually break something while working on a project which also involves other people.
EDIT: Providing example for my comment bellow.
Project = {
// All the variables/constants/objects that need to be globally accessible inside the Project object.
init : function(){
// Main entry point...
this.MainMenu.init();
// Rest of the code which should execute the moment Project is initiated.
}
}
Project.MainMenu = {
// All the variables/constants/objects that need to be accessible only to MainMenu.
init : function(){ // Is run immediatelly by Project.init()
// Event handlers relevant to the main menu are bound here
// Rest of the initialization code
}
}
Project.SlideShow = {
// All the variables/constants/objects that need to be accessible only to SlideShow.
init : function(){ // Is run only on pages that really require it.
// Event handlers for the slideshow.
}
}