External js load via ajax?

I am working with the Yahoo Maps API and I’d like to only include the
Yahoo javascript library on demand. I have a link_to_remote that shows a
Yahoo Map. Rather than the Yahoo library on every page it
seems more efficient to load this as part of my RJS partial (“on
demand”).

Is this possible? It doesn’t seem like it. Can be honored
by a browser when returned from an AJAX call?

When I load the Yahoo js library as part of the regular HTTP page it
works fine.

Thanks!

Carl J. wrote:

Is this possible? It doesn’t seem like it. Can be honored
by a browser when returned from an AJAX call?

Don’t see why that wouldn’t work. Just pass back something like:

script = document.createElement(‘script’);
script.id = ‘newScript’;
script.type = ‘text/javascript’;
script.src = ‘yahoo_stuffs.js’;
document.getElementsByTagName(‘head’)[0].appendChild(script);

or the shorter prototype way:

new Insertion.Bottom(‘id_of_place_u_want_it’, script)

or the RJS way:

insert RJS way here…

Dave C. wrote:

or the shorter prototype way:
new Insertion.Bottom(‘id_of_place_u_want_it’, script)

guess thats really not shorter :slight_smile:

or the RJS way:

insert RJS way here…

I’m using RJS. It’s quite simple actually… I have my Yahoo Map stuff
all in a partial, so with RJS I simply have:

def ymap
render :update do |page|
page.replace_html ‘map’, :partial => ‘ymap’, :locals => {:place =>
Place.find(params[:id])}
end
end

The javascript include stuff is at the top of the partial.
The browser doesn’t seem to be obeying it though.

I’ll try some different methods such as what you suggested.

Carl J. wrote:

This does not seem to work. For one thing I can’t find any reference to
“type” or “src” methods for DOM elements.

This works just fine. If you have firefox developer toolbar installed,
go to the view source tab, then “view generated source” and it will show
you that the

You are creating a new page element and assigning it to the variable
script. id, type and src are attributes you’re setting on that page
element object.

you could do: script.madeupcrap = “stuff”; and it would write out an
attribute like:

Copy and paste the code into just an html doc and run it in your
browser. If you’re scripts aren’t being eval-ed on return of your
link_to_remote call, that might be the issue.

Set your RJS return to do an alert or something to let you know the
request was sent back: alert(‘i worked, yay!’); or something.

Also, the Firebug plugin for Firefox is great to debug ajax requests
with. Shows you what happened round trip.

Hope u get it working :slight_smile:

Dave C. wrote:

Carl J. wrote:

This does not seem to work. For one thing I can’t find any reference to
“type” or “src” methods for DOM elements.

This works just fine. If you have firefox developer toolbar installed,
go to the view source tab, then “view generated source” and it will show
you that the tag was appended to the bottom of the tag.

Yeah, it actually sort of works… but not with Safari. I found a couple
threads on a Prototype mailing list discussing it.

At any rate, I may just leave it in the original page. It should be
cached I believe, so I’m now wondering if it’s worth all the trouble…

Dave C. wrote:

Don’t see why that wouldn’t work. Just pass back something like:

script = document.createElement(‘script’);
script.id = ‘newScript’;
script.type = ‘text/javascript’;
script.src = ‘yahoo_stuffs.js’;
document.getElementsByTagName(‘head’)[0].appendChild(script);

This does not seem to work. For one thing I can’t find any reference to
“type” or “src” methods for DOM elements.

or the shorter prototype way:

new Insertion.Bottom(‘id_of_place_u_want_it’, script)

This does not work either. Apparently prototype doesn’t support external
scripts. There’s a patch for it but Rails/Prototype don’t seem to be
interested:

http://dev.rubyonrails.org/ticket/6722

It does work fine when I include the javascript on the original page
whether a map is ever loaded or not, but there’s a noticeable hit on the
page loading times. :frowning: