Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #26568
| From | Joao Rodrigues <groups_jr-1@yahoo.com.br> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Dynamically add/remove code in browsers |
| Date | 2015-01-13 12:17 -0200 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <m939de$tpk$1@speranza.aioe.org> (permalink) |
| References | <m905b3$90j$1@dont-email.me> <11460823.sCPipJV62P@PointedEars.de> |
Thomas Lahn wrote:
> You cannot assume that the script has been loaded and executed once you
> inserted the “script” element in the document.
>
> You need to listen to and handle the “load” event of the “script” element:
>
> sc.onload = function () {
> el.onclick = handleClick;
> };
>
> But it is presently unknown to me where this feature now standardized in
> HTML5 is supported. Thus, it may be necessary to use setTimeout()-based
> polling to determine if execution has reached the end of the loaded script
> before you continue.
onload is not supported in IE8 and below. So, in this case, one could
use the readystatechange event. E.g.:
// IE8 and below
if (sc.readyState) {
sc.onreadystatechange = function () {
if (sc.readyState == "loaded" || sc.readyState == "complete") {
script.onreadystatechange = null;
el.onclick = handleClick;
}
};
} else { // FF, Chrome, Safari, Opera, etc.
sc.onload = function () {
el.onclick = handleClick;
};
}
An alternate method would be XMLHttpRequest() which works in all modern
browsers. A caveat is the same-origin policy, which prevents downloading
the script from CDNs for instance.
>
> Also, you can use the “document.head” property if it is available:
>
> (document.head || document.getElementsByTagName("head")[0])
> .appendChild(sc);
>
> It might be easier to use the backwards-compatible “document.body” property
> instead:
>
> document.body.appendChild(sc);
>
document.body.appendChild(sc) could be problematic in older versions of
IE. See
<http://www.nczonline.net/blog/2008/03/17/the-dreaded-operation-aborted-error/>
--
Joao Rodrigues
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Dynamically add/remove code in browsers "Leonardo Azpurua" <leonardo@exmvps.org> - 2015-01-12 05:22 -0430
Re: Dynamically add/remove code in browsers Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-12 17:21 +0100
Re: Dynamically add/remove code in browsers Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2015-01-12 16:44 -0200
Re: Dynamically add/remove code in browsers Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-12 22:01 +0100
Re: Dynamically add/remove code in browsers Joao Rodrigues <groups_jr-1@yahoo.com> - 2015-01-12 22:06 -0200
Re: Dynamically add/remove code in browsers Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-13 01:11 +0100
Re: Dynamically add/remove code in browsers "Leonardo Azpurua" <leonardo@exmvps.org> - 2015-01-12 15:21 -0430
Re: Dynamically add/remove code in browsers Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-12 22:03 +0100
Re: Dynamically add/remove code in browsers Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2015-01-13 12:17 -0200
Re: Dynamically add/remove code in browsers Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-13 20:50 +0100
csiph-web