On Tue, Dec 6, 2011 at 6:29 PM, S.A. <game...@gmail.com> wrote:
>> Ok, it is inserted at the right place, but are you sure, that there
>> will be a readstatechange event? Didn't it already fired by the time
>> that you insert your script? Also if you ever looked up the jQuery
>> source, you can see, that when you attach a function to the ready
>> event, it will fire "immediately" if the page is already in a ready
>> state.
>
> 'script' node insertion suppose to lead to 'readystatechange' state change
> in IE and onload
> function call in FF/Chrome. It appears that these only work if you use 'src'
> attribute and
> download the script. Supplying the code directly does not seem to have
> effect on these
> events!
>

The example and the use of fragments and a nested "div" seems too
intricate for such a simple task.

You can easily achieve that (load event firing) in
Firefox/Safari/Chrome/Opera and newer browsers by building and passing
a data URI string in the "src" attribute of the script element:

(function() {

  var elem = document.createElement('script');
  elem.setAttribute('type', 'text/javascript');
  elem.setAttribute('src', 'data:text/javascript,' + 'alert(1)');
  document.addEventListener('load', loadFunc, true);
  document.documentElement.insertBefore(elem, null);

  function loadFunc() {
    console.log("file got loaded");
  }

})();

The 'alert(1)' string in the above example represents your Ajax
response ("res.responseText").

Notice that I use a "capturing" event listener; if there are many
external resources, you should check for the exact one you need in the
"loadFunc()" function, the load event may be fired by any external
resources or script injections.

--
Diego


> --
> To view archived discussions from the original JSMentors Mailman list:
> http://www.mail-archive.com/jsmentors@jsmentors.com/
>
> To search via a non-Google archive, visit here:
> http://www.mail-archive.com/jsmentors@googlegroups.com/
>
> To unsubscribe from this group, send email to
> jsmentors+unsubscr...@googlegroups.com

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/jsmentors@jsmentors.com/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/jsmentors@googlegroups.com/

To unsubscribe from this group, send email to
jsmentors+unsubscr...@googlegroups.com

Reply via email to