Friday, 15 March 2013

html - Why do JavaScript widgets (like Tweet Button and Facebook Like) add the script dynamically? -



html - Why do JavaScript widgets (like Tweet Button and Facebook Like) add the script dynamically? -

this question has reply here:

document.createelement('script') vs <script src=“”> 3 answers

both tweet button , facebook like widgets tell add together html:

class="lang-js prettyprint-override"><script> !function(d,s,id){ var js,fjs=d.getelementsbytagname(s)[0]; if(!d.getelementbyid(id)){ js=d.createelement(s); js.id=id; js.src="https://platform.twitter.com/widgets.js"; fjs.parentnode.insertbefore(js,fjs); } }(document,"script","twitter-wjs"); </script>

which dynamically adds <script> element dom loads actual widget javascript. why done dynamically instead of straight loading widget (which simpler , cleaner):

class="lang-html prettyprint-override"><script src="https://platform.twitter.com/widgets.js"></script>

the reason can think of defers script load until page has loaded, can accomplished placing <script> before </body> close.

because people implementing code can't guaranteed competent plenty place script in right place. additionally, script ideally universal plenty using scheme implement it.

some cmss don't allow fine-grained command of scripts added page. it's in best involvement of social networks provide code more people can utilize less confusion.

javascript html facebook twitter

No comments:

Post a Comment