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
</script>

Why use multiple tags? Why not:

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

Recommended Answers

All 4 Replies

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.

Dex09,

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.

Airshow

Thanks. That makes sense.

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

Dex, you will grow to love them.

Airshow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.