Prototype: Load <script> with src attribute using insert() at runtime

1 view
Skip to first unread message

wwarby

unread,
Jul 5, 2008, 6:08:39 AM7/5/08
to Ruby on Rails: Spinoffs
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.

wwarby

unread,
Jul 5, 2008, 7:21:43 AM7/5/08
to Ruby on Rails: Spinoffs
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 ;)
Reply all
Reply to author
Forward
0 new messages