2

I think I already understand what is happening, but I couldn't find any documentation on it so I need to know for sure:

Take this example from AddThis:

<script type="text/javascript">var addthis_config = {"data_track_addressbar":true};</script>
<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=example"></script>

I wonder why putting the config script into the src call is not allowed:

<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=example"> var addthis_config = {"data_track_addressbar":true}; </script>

My thoery is that when the src is called, it actually overwrites the code that is written between the tags, but what is the official reasoning and behavior?

RenaissanceProgrammer
  • 404
  • 1
  • 11
  • 30

1 Answers1

2

From: http://www.jsworkshop.com/articles/02scriptsrc.html

The closing tag is required, and you should not use JavaScript commands between the tags.

From: http://javascript.crockford.com/script.html

The src attribute is optional. If it is present, then its value is a url which identifies a .js file. The loading and processing of the page pauses while the browser fetches, compiles, and executes the file. The content between the <script src="url"> and the </script> should be blank.

If the src attribute is not present, then the content text between the <script> and the </script> is compiled and executed.

No explanation of why, but my guess is that a valid src disables the execution of anything between the tags.

Also, see this answer: https://stackoverflow.com/a/6528343/637283

Community
  • 1
  • 1
Mike Lyons
  • 1,748
  • 2
  • 20
  • 33