# External js load via ajax?

**URL:** <https://www.ruby-forum.com/t/external-js-load-via-ajax/111782>\
**Category:** Rails\
**Created:** [August 10, 2007, 6:57pm UTC](https://www.ruby-forum.com/t/external-js-load-via-ajax/111782 "2007-08-10T18:57:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cjnkns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c5a1d2/32.png) [@cjnkns](https://www.ruby-forum.com/u/cjnkns)\
**Post date:** [August 10, 2007, 6:57pm UTC](https://www.ruby-forum.com/t/external-js-load-via-ajax/111782/1 "2007-08-10T18:57:23Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![cjnkns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c5a1d2/32.png) [@cjnkns](https://www.ruby-forum.com/u/cjnkns)\
**Post date:** [August 10, 2007, 8:36pm UTC](https://www.ruby-forum.com/t/external-js-load-via-ajax/111782/2 "2007-08-10T20:36:27Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![cjnkns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c5a1d2/32.png) [@cjnkns](https://www.ruby-forum.com/u/cjnkns)\
**Post date:** [August 10, 2007, 8:42pm UTC](https://www.ruby-forum.com/t/external-js-load-via-ajax/111782/3 "2007-08-10T20:42:15Z")

</div>

Dave C. wrote:

> or the shorter prototype way:  
> new Insertion.Bottom(‘id\_of\_place\_u\_want\_it’, script)

guess thats really not shorter 🙂

---

<div class="post-metadata">

**Author:** ![cjnkns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c5a1d2/32.png) [@cjnkns](https://www.ruby-forum.com/u/cjnkns)\
**Post date:** [August 10, 2007, 8:44pm UTC](https://www.ruby-forum.com/t/external-js-load-via-ajax/111782/4 "2007-08-10T20:44:42Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![cjnkns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c5a1d2/32.png) [@cjnkns](https://www.ruby-forum.com/u/cjnkns)\
**Post date:** [August 11, 2007, 3:56am UTC](https://www.ruby-forum.com/t/external-js-load-via-ajax/111782/5 "2007-08-11T03:56:02Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![cjnkns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c5a1d2/32.png) [@cjnkns](https://www.ruby-forum.com/u/cjnkns)\
**Post date:** [August 11, 2007, 7:43am UTC](https://www.ruby-forum.com/t/external-js-load-via-ajax/111782/6 "2007-08-11T07:43:56Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![cjnkns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c5a1d2/32.png) [@cjnkns](https://www.ruby-forum.com/u/cjnkns)\
**Post date:** [August 10, 2007, 11:48pm UTC](https://www.ruby-forum.com/t/external-js-load-via-ajax/111782/7 "2007-08-10T23:48:31Z")

</div>

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](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. ☹
