javascript - Why do developers write these type of scripts for embedding when a simple tag would do? -


i wanted ask question practice have seen quite lot regarding jquery/javascript plugins/widgets written other developers.

i maintain number of sites , asked add new scripts various things such chat widgets, marketing tools etc etc code sent majority of these looks this.

<script type="text/javascript">  (function() {  var hm = document.createelement('script'); hm.type ='text/javascript';     hm.async = true;  hm.src = ('++www-widget-org+widget-js').replace(/[+]/g,'/').replace(/-/g,'.');  var s = document.getelementsbytagname('script')[0];     s.parentnode.insertbefore(hm, s);  })();  </script> 

now question why go trouble of writing above script when need this?

<script src="//www.widget.org/widget.js" type="text/javascript" async></script> 

i have seen number of times , beginning wonder benefit of approach is? if there one?

according steve souders, method used load scripts in non-blocking fashion. makes sense given these scripts supplemental page , not directly related function (why make site slow because added google analytics, etc.).

there's other scripts use opportunity initialize basic data (e.g. googleanalytics uses _setaccount). i've seen scripts aggregate user statistics (screen size, agent, , other client data) , pass them off remote script (in query parameters can later review/analyze gain detail demographic).


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -