Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

document.write('<scr' + 'ipt src= vs <script src=

Tags:

javascript

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

like image 936
Denis Hoctor Avatar asked Feb 10 '10 11:02

Denis Hoctor


2 Answers

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

like image 113
SF. Avatar answered Sep 30 '22 18:09

SF.


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/

like image 20
hojberg Avatar answered Sep 30 '22 19:09

hojberg