Just a simple question, this has always bugged me.

I always wondered why JS includes and code were split into separate script tags. jQuery, Analytics, and others do this:

<script src="/js/scriptname.js" type="text/javascript"></script>
<script type="text/javascript">
   //code here

Why use multiple tags? Why not:

<script src="/js/scriptname.js" type="text/javascript">
   //code here
7 Years
Discussion Span
Last Post by Airshow

it is because in xml, nodes can either be terminal or not. when you specify a src attribute for a script tag, it becomes a terminal node which means you can not add any child to it, when you add a child, the browser basically ignores the content. html is not fully qualified xml so such bugs exist. that is why they try to make something like xhtml. but still, it is not a fully qualified xml document.



As Serkan indicates, in your second example the enclosed code would be neither parsed nor executed.

Typically, <script> tags with a src are used to include blocks of code developed to provide functionality (or data) to more than one web page. Because the src attrubute is a URL, the included code can be anywhere on the www, not necessarily on the same domain as the <script> tag's containing page.

<script> tags without a src attribute contain code that is available only to the page in which it sits.

Hope this helps.



Thanks. That makes sense.

Guess I'll just have to deal with all the script tags then.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.