tags:

views:

166

answers:

3

Hi all,

Apart from allowing you insert js variables into a script tag when written like document.write('<scr' + 'ipt src= what are the pros/cons of this vs a normal <script src=> tag?

I'm mainly asking with regard to speed but interested in the whole story.

Thanks Denis

A: 

Other than those? There aren't any.

(Incidentally, splitting a script tag in a JS string into a pair of concatenated strings is pointless bloat)

David Dorward
+8  A: 

There is no need for '<scr'+'ipt'. There is need for '<\/scr'+'ipt>'. Because HTML interpreter has no need to understand Javascript, so it will treat everything between <script>...</script> as the text, and won't care var a='</script>'; is a string literal Javascript, it will consider it the closing tag for <script> and regard the remainder of the script text as plain (erroneous) HTML.

edit: corrected per David's suggestion

SF.
And that's wrong too. It should be: `"<\/script>"` since HTML parsers (at least ones which implement HTML correctly, i.e. not most web browsers) will treat `</` as an end tag.
David Dorward
`"<\/script>"` not `"<\/scr" + "ipt>"`. The latter is pointless, inefficient, hard to read bloat.
David Dorward
+1  A: 

I assume this is to gain non blocking javascript loading.

For this i suggest looking at Steve Souders posts about the subject. http://www.stevesouders.com/blog/2009/04/27/loading-scripts-without-blocking/

The LABjs library solves this in a pretty nifty way. http://labjs.com/

Also it seems newer browsers are beginning to load things parallel by default http://www.stevesouders.com/blog/2010/02/07/browser-script-loading-roundup/

hojberg