Forum: Rails Spinoffs (closed, excessive spam) Prototype: Load <script> with src attribute using insert() at runtime

Posted by wwarby (Guest)
on 2008-07-05 12:09
(Received via mailing list)
Hi all,

Using the latest version of Prototype, I'm trying to dynamically load
JavaScripts at runtime on demand, into the head or body tags (I don't
actually care which). The insert() method just seems to do nothing
when I specify a script tag containing a src attribute:

$$('body')[0].insert('<script type="text/javascript"
src="dynamic.js"></script>');

However, if I omit the src tag, instead using in-line JavaScript
inside the tag, it loads correctly:

$$('body')[0].insert('<script type="text/javascript">alert("foo");</
script>');

Is this some kind of browser security feature or a bug, or am I just
doing something goofy? I could get the contents of the file using a
server script but that would be a very dirty solution that I'd like to
avoid if possible.

Grateful for any advice received.
Posted by wwarby (Guest)
on 2008-07-05 13:22
(Received via mailing list)
Apologies, I see this question has been posted several times before. I
have solved the problem using document.createElement to manally create
the script element and that seems to work fine ;)
This topic is locked and can not be replied to.