Documentation: SCRIPT Tag Merging

Unique by SRC, or Not

Every original SCRIPT tag in a resource document's HEAD tag that has a unique SRC attribute value is imported into the document's Master Page. As with LINK element merging, this comparison is case-sensitive and is not canonicalized.

All SCRIPT tags in the resource document's HEAD tag that have no SRC attribute (presumably because they contain bare script code) is imported verbatim. SCRIPT tags that are imported "closest" to the resource document appear "latest" in the HEAD tag of the final merged document. This enables web page authors to set up predictable script dependencies and this effect is propagated along a chain of Master Pages, if one exists.

SCRIPT tags not in the HEAD tag of merging documents are ignored but can be handled like any other content according to the relationship between HTML Master Page's SSI-Container and SSI-Filler directives, if you really need to place script code outside the HEAD tag of your documents.

Examples

  1. Duplication of the SRC attribute value of SCRIPT tags results in only the "first" instance appearing in the final merged document:
    <script type="text/javascript" src="/script/common.js"><script>
    + <script type="text/javascript" src="/script/common.js"><script>
    = <script type="text/javascript" src="/script/common.js"><script>
  2. Sourcing scripts that are not sourced by the Master Page (or any of its chained Master Pages, if a chain exists) results in all original scripts being sourced; scripts closest to the requested resource document appear "later" in the final merged document's HEAD tag, so it is possible to design and reliably anticipate the execution order of your scripts:
    <script type="text/javascript" src="/script/depends-on-common.js"><script>
    + <script type="text/javascript" src="/script/common.js"><script>
    =
    <script type="text/javascript" src="/script/common.js"><script>
    <script type="text/javascript" src="/script/depends-on-common.js"><script>
  3. Writing bare SCRIPT tags results in every one of them being imported verbatim into the final merged document. Such scripts are ordered such that those closest to the requested resource document always appear "later" in the resulting merged HEAD tag:
    <script type="text/javascript">
       alert(SOME_GLOBAL_STRING);
    <script>
    +
    <script type="text/javascript">
       var SOME_GLOBAL_STRING = 'Your common message here, in a Master Page!';
    <script>
    =
    <script type="text/javascript">
       var SOME_GLOBAL_STRING = 'Your common message here, in a Master Page!';
    <script>
    <script type="text/javascript">
       alert(SOME_GLOBAL_STRING);
    <script>
  4. Both bare and sourced SCRIPT tags can be used together with anticipatable results:
    <script type="text/javascript">
       alert(SOME_GLOBAL_STRING);
    <script>
    + <script type="text/javascript" src="/script/sets-that-global-string.js"><script>
    =
    <script type="text/javascript" src="/script/sets-that-global-string.js"><script>
    <script type="text/javascript">
       alert(SOME_GLOBAL_STRING);
    <script>