# Inserting Javascript dinamically with DOM

**URL:** <https://www.ruby-forum.com/t/inserting-javascript-dinamically-with-dom/183193>\
**Category:** Rails\
**Created:** [February 9, 2010, 8:56pm UTC](https://www.ruby-forum.com/t/inserting-javascript-dinamically-with-dom/183193 "2010-02-09T20:56:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![denommus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e99b99/32.png) [@denommus](https://www.ruby-forum.com/u/denommus)\
**Post date:** [February 9, 2010, 8:56pm UTC](https://www.ruby-forum.com/t/inserting-javascript-dinamically-with-dom/183193/1 "2010-02-09T20:56:40Z")

</div>

I want to do simple stuff. In my controller I have this:

render :update do |page|  
page.replace\_html ‘scripts’, :partial =\> ‘scripts’, :locals =\>  
{:question =\> @question}  
end

And in the partial I have this:

\<% if question.sound %\>  
Sound loaded!

\<% end %\>

Well, when I run it, the message “Sound loaded!” appears, but the  
scripts are not rendered (I inspect the elements with Chrome and with  
Firebug, and the scripts do not appear).

Is there anything wrong when I try to do this? Is there a way to make  
this work?

---

<div class="post-metadata">

**Author:** ![denommus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e99b99/32.png) [@denommus](https://www.ruby-forum.com/u/denommus)\
**Post date:** [February 9, 2010, 9:01pm UTC](https://www.ruby-forum.com/t/inserting-javascript-dinamically-with-dom/183193/2 "2010-02-09T21:01:34Z")

</div>

Yuri Albuquerque wrote:

> I want to do simple stuff. In my controller I have this:
> 
> render :update do |page|  
> page.replace\_html ‘scripts’, :partial =\> ‘scripts’, :locals =\>  
> {:question =\> @question}  
> end
> 
> And in the partial I have this:
> 
> \<% if question.sound %\>  
> Sound loaded!
> 
> \<% end %\>
> 
> Well, when I run it, the message “Sound loaded!” appears, but the  
> scripts are not rendered (I inspect the elements with Chrome and with  
> Firebug, and the scripts do not appear).
> 
> Is there anything wrong when I try to do this? Is there a way to make  
> this work?

Why not just call the script file from a tag instead of  
replace\_html? In general, your JavaScript should be coming from static  
external files.

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![denommus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e99b99/32.png) [@denommus](https://www.ruby-forum.com/u/denommus)\
**Post date:** [February 10, 2010, 5:37pm UTC](https://www.ruby-forum.com/t/inserting-javascript-dinamically-with-dom/183193/3 "2010-02-10T17:37:58Z")

</div>

> Why not just call the script file from a
> 
> And this on the controller:
> 
> ```
> page << 'soundManager.unload();'
> page << "q_sound = '#{url_for_file_column('question', :sound)}';"
> page << 'soundManager.load();'
> 
> ```
> 
> But when I test, it return this error:
> 
> RJS error:
> 
> Error: SoundManager.getSoundById(): sID is null/undefined
> 
> I know why this error is ocurring (because soundManager is trying to  
> load q\_sound even if it is empty), but I don’t know how to solve.
> 
> Any tips?

---

<div class="post-metadata">

**Author:** ![denommus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e99b99/32.png) [@denommus](https://www.ruby-forum.com/u/denommus)\
**Post date:** [February 10, 2010, 5:54pm UTC](https://www.ruby-forum.com/t/inserting-javascript-dinamically-with-dom/183193/4 "2010-02-10T17:54:07Z")

</div>

Yuri Albuquerque wrote:

> > Why not just call the script file from a
> > 
> > And this on the controller:
> > 
> > ```
> > page << 'soundManager.unload();'
> > page << "q_sound = '#{url_for_file_column('question', :sound)}';"
> > page << 'soundManager.load();'
> > 
> > ```
> > 
> > But when I test, it return this error:
> > 
> > RJS error:
> > 
> > Error: SoundManager.getSoundById(): sID is null/undefined
> > 
> > I know why this error is ocurring (because soundManager is trying to  
> > load q\_sound even if it is empty), but I don’t know how to solve.
> > 
> > Any tips?
> 
> Follow the advice I already gave.
> 
> Best,  
> –Â  
> Marnen Laibow-Koser  
> [http://www.marnen.org](http://www.marnen.org)  
> removed\_email\_address@domain.invalid
