admin管理员组

文章数量:1336164

this is my code :

<p onload=javascript:alert('sss')>www</p>

and the code cant alert'sss',

what's wrong with my code,

thanks

this is my code :

<p onload=javascript:alert('sss')>www</p>

and the code cant alert'sss',

what's wrong with my code,

thanks

Share Improve this question asked Feb 15, 2011 at 5:37 zjm1126zjm1126 35.7k53 gold badges125 silver badges167 bronze badges 2
  • need quotes around onload and javascript: is not required. – bhups Commented Feb 15, 2011 at 5:39
  • 2 (An HTML validator will say what is wrong for the given doc-type). – user166390 Commented Feb 15, 2011 at 7:46
Add a ment  | 

5 Answers 5

Reset to default 3

You can try this with body element. Because the load event is fired when the whole document is loaded; I did not find anything about element-specific load events. So I assume that element1.onload is triggered by the same event as element2.onload - the following two bodies would be equivalent:

<body>
  <p onload="javascript:alert('sss')">Text</p>
  <p onload="javascript:alert('sss')">Text</p>
</body>


<body onload="javascript:alert('sss')">
  <p>Text</p>
  <p>Text</p>
</body>

PS. that the onload event handler is now available for every HTML element in HTML5

As far as I know, onload event can only be used with body or frameset tag. You cannot use this event with p tag.

For further references, go here.

You'd be better served to move the javascript to the <body> tag:

<body onload="alert('sss');">

IIRC, onload only works reliably on the <body> element.

You should have quotes around the onload and have a semicolon.

<p onload="javascript:alert('sss');">www</p>

It should also be noticed that this is not W3C standard and the onload should really be added to the body tag.

本文标签: htmlhow to run this javascript in the ltpgt element Stack Overflow