开发者

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

开发者 https://www.devze.com 2022-12-19 14:15 出处:网络
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?

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 inte开发者_运维技巧rested in the whole story.

Thanks Denis


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


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/


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消