dom - JavaScript insert script with async and use functions from it? -


this question has answer here:

i have written small widget include onto pages this:

<script type="text/javascript"> var _sid = '1';   (function() {     var se = document.createelement('script'); se.type = 'text/javascript'; se.async = true;     se.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://') + 'dev.domain.com/widget/hello.js';     var s = document.getelementsbytagname('script')[0]; s.parentnode.insertbefore(se, s);   })();   </script> 

now want find way call functions exist within hello.js, looks this:

var widget = function () {      function setname(a) {         console.log(a);     }      return widget;   }(); 

so want able make call setname so: widget.setname("andy")

...from embed page, reason "widget undefined."

any ideas doing wrong?

before use widget, need know loaded, there multiple options that:

  1. use requirejs or it.

  2. implement callback function in javascript loading, fire 'document.onhelloisloaded', if want know if hello loaded, set variable function.

  3. make loop, checking if widget variable set. setinterval (which cancels when widget found , code has executed)

you need make setname variable accessible when have access widget, isn't. should put this:

var widget = {}; widget.setname = function()...  return widget; 

Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -