should I add "javascript:" in front of html tag function? [duplicate]
Possible Duplicate:
When do I need to specify the JavaScript protocol?
for example
<body onload="javascript:something();">
in this code, should I put javascript:
?
some of the codes attatch javascript:
,
but some don't.
what is the safe and correct?
A better solution would be to avoid explicit use of JavaScript in your markup altogether and to use something like jQuery to extract it all to a separate file; you can then do something like this:
$(function()
{
// This will be run when the document is loaded.
alert('foo');
$('#some-link').click(function()
{
// This will be run when the element with id `some-link` is clicked.
alert('bar');
});
});
No. Just use your javascript.
<body onload="something();">
javascript:
can be used with the href
attribute of a
element.
<a href="javascript:something();">
But just for the protocol, I prefer using the onload
method.
I'd suggest something like
<body>
...
<script>
function something() {}
window.onload = something;
</script>
精彩评论