Jump to content

<script> Tag Won't Validate If Contains Onload


CharlesHarrison
 Share

Recommended Posts

I don't see why you need an onload attribute on the script tag anyways. You could just put whatever code you want at the end of the script and it should work.

<script type="text/javascript" onload="alert(1)">function X() {  // Some code}</script>

is the same as

<script type="text/javascript">function X() {  // Some code}alert(1);</script>

Link to comment
Share on other sites

I don't see why you need an onload attribute on the script tag anyways. You could just put whatever code you want at the end of the script and it should work.
But if the js is pulled in asynchronously from a third party one might need to know when it has finished loading. Also, the problem is not how to do it, I've already got an example to work.I want to know which is correct, W3Schools or the online validating tool. Is it valid Transitional to include an onload attribute to a script tag, or not?
Link to comment
Share on other sites

The W3C specification says that the onload attribute can only be used on the <body> and <frameset> elements.I'm surprised, as I was quite sure the <img> element also supported it.

Link to comment
Share on other sites

The W3C specification says that the onload attribute can only be used on the <body> and <frameset> elements.I'm surprised, as I was quite sure the <img> element also supported it.
Thanks. That explains why the validator is rejecting it then. Perhaps W3Schools should update their documentation?
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...