# Can RJS render new javascript?

**URL:** <https://www.ruby-forum.com/t/can-rjs-render-new-javascript/164308>\
**Category:** Rails\
**Created:** [March 31, 2009, 3:24am UTC](https://www.ruby-forum.com/t/can-rjs-render-new-javascript/164308 "2009-03-31T03:24:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [March 31, 2009, 3:24am UTC](https://www.ruby-forum.com/t/can-rjs-render-new-javascript/164308/1 "2009-03-31T03:24:21Z")

</div>

Hi -

I’m trying to create a widget where my rails code generates a fresh  
widget (which is javascript) to replace the old one. To do this, I  
need to insert new javascript with AJAX with RJS (so my partial begins  
with \<script…). Is this possible? Right now, it replaces the old  
widget div with nothing and I get no errors. Is this a browser  
safeguard or something?

Thanks for any help,  
Dino

---

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [March 31, 2009, 3:40am UTC](https://www.ruby-forum.com/t/can-rjs-render-new-javascript/164308/2 "2009-03-31T03:40:10Z")

</div>

dino d. wrote:

> I’m trying to create a widget where my rails code generates a fresh  
> widget (which is javascript) to replace the old one. To do this, I  
> need to insert new javascript with AJAX with RJS (so my partial begins  
> with \<script…). Is this possible? Right now, it replaces the old  
> widget div with nothing and I get no errors. Is this a browser  
> safeguard or something?

Wouldn’t your controller action look like this?

def xhr\_whatever  
render :update do |rjs|  
rjs.replace :my\_div, ’’  
end  
end

The stack should handle escaping, javascript-ing, escaping,  
transmitting,  
de-escaping, evaluating, and de-escaping that.

But I’m sure there’s a better way to write your actual feature. I sling  
Ajax  
here and there all the time and never once found a need to push a  
‘’…

---

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [March 31, 2009, 5:12am UTC](https://www.ruby-forum.com/t/can-rjs-render-new-javascript/164308/3 "2009-03-31T05:12:41Z")

</div>

Thanks for the reply. I’m writing the code because i’m  
testing an example user of the widget, and it simply calls the website  
that generates the widget. so is there some escaping type issue that  
would prevent directly rendering a tag from RJS?

---

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [March 31, 2009, 10:55am UTC](https://www.ruby-forum.com/t/can-rjs-render-new-javascript/164308/4 "2009-03-31T10:55:27Z")

</div>

On Mar 31, 4:12 am, dinoD [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks for the reply. I’m writing the code because i’m  
> testing an example user of the widget, and it simply calls the website  
> that generates the widget. so is there some escaping type issue that  
> would prevent directly rendering a tag from RJS?

Prototype strips script tags (see  
[http://prototypejs.org/api/element/replace](http://prototypejs.org/api/element/replace)  
)

Fred

---

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [March 31, 2009, 5:16pm UTC](https://www.ruby-forum.com/t/can-rjs-render-new-javascript/164308/5 "2009-03-31T17:16:14Z")

</div>

On Mar 31, 4:03 pm, dinoD [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks Fred. I see in the code that you’re correct (however that link  
> implies that you can do this, it shows an example of how to add a  
> script tag). So generically, is it ever possible to use an ajax call,  
> such as link\_to\_remote, where response partial renders javascript?

I suppose you could just assign stuff to element.innerHTML without  
using prototype. ALso prototype does actually eval the script tags, it  
just doesn’t leave them hanging around in the DOM.

Fred

---

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [March 31, 2009, 10:53pm UTC](https://www.ruby-forum.com/t/can-rjs-render-new-javascript/164308/6 "2009-03-31T22:53:57Z")

</div>

Sorry for the dumb follow up, but can you do that in RJS (update an  
elements innerHTML directly) ?

Thanks Fred,  
Dino

On Mar 31, 11:15 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [March 31, 2009, 5:03pm UTC](https://www.ruby-forum.com/t/can-rjs-render-new-javascript/164308/7 "2009-03-31T17:03:55Z")

</div>

Thanks Fred. I see in the code that you’re correct (however that link  
implies that you can do this, it shows an example of how to add a  
script tag). So generically, is it ever possible to use an ajax call,  
such as link\_to\_remote, where response partial renders javascript?

Thanks,  
Dino

On Mar 31, 4:54 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [March 31, 2009, 11:36pm UTC](https://www.ruby-forum.com/t/can-rjs-render-new-javascript/164308/8 "2009-03-31T23:36:42Z")

</div>

On Mar 31, 9:53 pm, dinoD [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Sorry for the dumb follow up, but can you do that in RJS (update an  
> elements innerHTML directly) ?

Well there’s the assigns thing in RJS, failing that you can execute  
arbitrary javascript with \<\<

Fred
