开发者

should I add "javascript:" in front of html tag function? [duplicate]

This que开发者_StackOverflow社区stion already has answers here: Closed 12 years ago.

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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜