8

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

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
Denis Hoctor
  • 2,597
  • 4
  • 33
  • 51
  • I don't see any question – Andreas Bonini Feb 10 '10 at 11:24
  • Stackoverflow does this: `document.write(' – Skilldrick Feb 10 '10 at 11:31
  • Does this question help? http://stackoverflow.com/questions/236073/why-split-the-script-tag-when-writing-it-with-document-write – Paul D. Waite Feb 10 '10 at 11:36
  • @Paul. not really as I get why we split it. What I'm wondering is if I have a choice to split it and use a js variable or not split it and use a serverside variable, apart from reduced code bloat do I get anything inway of a speed increase or otherwise by going the server option? – Denis Hoctor Feb 10 '10 at 11:51

3 Answers3

9

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.
  • 13,549
  • 14
  • 71
  • 107
  • 4
    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. – Quentin Feb 10 '10 at 11:40
  • 3
    `"<\/script>"` not `"<\/scr" + "ipt>"`. The latter is pointless, inefficient, hard to read bloat. – Quentin Feb 10 '10 at 11:53
2

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
  • 1,074
  • 2
  • 9
  • 12
0

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)

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335