Possible Duplicate:
Do you ever need to specify javascript: in an onclick?
What is better to write in MVC3 when on the onclick event for example we call function with functionName()
? javascript:functionName()
or just functionName()
?
Possible Duplicate:
Do you ever need to specify javascript: in an onclick?
What is better to write in MVC3 when on the onclick event for example we call function with functionName()
? javascript:functionName()
or just functionName()
?
Actually the best is to not mix javascript and markup. So you could attach this onclick event handler unobtrusively using javascript in a separate file. But as far as your question is concerned, you don't need the javascript:
prefix.
javascript:
means different things depending on where it appears.
In a URI, it means to execute the JavaScript to get the resource. This is not progressive or unobtrusive and should be avoided.
In JavaScript (which the value of an onclick
attribute is), it is a label:
while
, do
, for
and switch
.while
, do
, for
or switch
in an onclick attribute, then you need to break it out into a function for the sake of your own sanity when you come back to read it later.It's better to not use javascript in your HTML, for separation of concerns and unobtrusive javascript.
If you want to add an onclick event, use the following:
document.getElementById('element').onclick = functionName
In your javascript file. This way, the presentation layer (HTML) is separated from the logic layer (Javascript).
Separating javascript from markup (aka behavioral separation or unobtrusive javascript) is the way to go. When you put your javascript within the markup (as in <div onclick="[javascript:]startMyFunction()" ...>
), the expression has to be eval
-ed, which spawns another interpreter.
So it's more efficient to put the scripting where it belongs: within the javascript tag.