# Replace\_html + javascript

**URL:** <https://www.ruby-forum.com/t/replace-html-javascript/99200>\
**Category:** Rails\
**Created:** [July 20, 2006, 10:47pm UTC](https://www.ruby-forum.com/t/replace-html-javascript/99200 "2006-07-20T22:47:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/cc9497/32.png) [@hax](https://www.ruby-forum.com/u/hax)\
**Post date:** [July 20, 2006, 10:47pm UTC](https://www.ruby-forum.com/t/replace-html-javascript/99200/1 "2006-07-20T22:47:31Z")

</div>

Hi all,

This is my first post to the list, so I hope it gets through and I  
don’t do anything wrong 🙂

Here’s the issue I’m having…

I have an rjs that’s doing:  
page.replace\_html ‘myid’, :partial =\> ‘my\_partial’

And in my my\_partial I have:

And this breaks my replace\_html, and I get a javascript error in  
firebug that looks like it’s not escaping it right.

Does anyone else experience this? Anyone have any suggestions on how  
to fix it?

TIA  
–hax

---

<div class="post-metadata">

**Author:** ![hax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/cc9497/32.png) [@hax](https://www.ruby-forum.com/u/hax)\
**Post date:** [July 20, 2006, 11:17pm UTC](https://www.ruby-forum.com/t/replace-html-javascript/99200/2 "2006-07-20T23:17:56Z")

</div>

Can’t you just do

page.replace\_html ‘myid’, :partial =\> ‘my\_partial’  
page.alert(‘foo’)

in the RJS template and skip the javascript in the partial?

Zack

---

<div class="post-metadata">

**Author:** ![hax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/cc9497/32.png) [@hax](https://www.ruby-forum.com/u/hax)\
**Post date:** [July 20, 2006, 11:24pm UTC](https://www.ruby-forum.com/t/replace-html-javascript/99200/3 "2006-07-20T23:24:59Z")

</div>

Hi hax,

hax wrote:

> This is my first post to the list, so I hope it gets through and I don’t  
> do anything wrong 🙂

Welcome aboard!

> I have an rjs that’s doing:  
> page.replace\_html ‘myid’, :partial =\> ‘my\_partial’
> 
> And in my my\_partial I have:

If it makes sense to do it in the RJS template, you can just do  
page.alert(‘foo’). If that doesn’t make sense, posting more of the code  
might help you get a better answer.

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![hax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/cc9497/32.png) [@hax](https://www.ruby-forum.com/u/hax)\
**Post date:** [July 21, 2006, 2:18pm UTC](https://www.ruby-forum.com/t/replace-html-javascript/99200/4 "2006-07-21T14:18:45Z")

</div>

hax wrote:

> Let me know what you think.

Not quite the same thing, but as a data point: I have one app that uses  
a lot of Ajax editing. The link that shows initially is a  
link\_to\_remote. The method it points to renders javascript via render  
:template. The template it renders, in turn, calls replace\_html with a  
partial. The partial includes a form\_remote\_tag.

All of this works fine, no problems with the escaping or anything. It  
seems like a vaguely comparable situation, so I thought I’d mention it.

–Al Evans

---

<div class="post-metadata">

**Author:** ![hax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/cc9497/32.png) [@hax](https://www.ruby-forum.com/u/hax)\
**Post date:** [July 21, 2006, 6:29pm UTC](https://www.ruby-forum.com/t/replace-html-javascript/99200/5 "2006-07-21T18:29:15Z")

</div>

> Not quite the same thing, but as a data point: I have one app that  
> uses  
> a lot of Ajax editing. The link that shows initially is a  
> link\_to\_remote. The method it points to renders javascript via render  
> :template. The template it renders, in turn, calls replace\_html with a  
> partial. The partial includes a form\_remote\_tag.

> All of this works fine, no problems with the escaping or anything. It  
> seems like a vaguely comparable situation, so I thought I’d mention  
> it.

Aha. This isn’t exactly the same thing, but it got me thinking, and  
after a good night’s sleep, here’s what the problem actually was:

In my rjs I was rendering a partial that had tags in it…  
but this works if you just set up a test case. The problem actually  
was with Sean Treadway’s responds\_to\_parent (http://  
sean.treadway.info/articles/2006/05/29/iframe-remoting-made-easy) and  
my iframe trickery.

As it turns out, his plugin seems to have the escaping problem.

Sorry this wasn’t actually related to the core like I had originally  
thought, but thank you for prompting me to fix it 🙂

If anyone needs more details, feel free to mail me.

–hax

---

<div class="post-metadata">

**Author:** ![hax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/cc9497/32.png) [@hax](https://www.ruby-forum.com/u/hax)\
**Post date:** [July 21, 2006, 5:46am UTC](https://www.ruby-forum.com/t/replace-html-javascript/99200/6 "2006-07-21T05:46:06Z")

</div>

> Welcome aboard!

Thanks, this list looks really helpful 🙂

> > I have an rjs that’s doing:  
> > page.replace\_html ‘myid’, :partial =\> ‘my\_partial’
> > 
> > And in my my\_partial I have:
> 
> If it makes sense to do it in the RJS template, you can just do  
> page.alert(‘foo’). If that doesn’t make sense, posting more of the  
> code might help you get a better answer.

I knew I should have posted more code 🙂

It actually looks more like this:

RJS: page.replace\_html ‘myid’, :partial =\> ‘my\_partial’

\_my\_partial.rhtml:  
@foo.each do |f|

"\> \<%= f.my\_string %\>

 \<%= in\_place\_editor "foo\_#{f.id}", :url =\> { :action =\> 'update\_my\_string', :id =\> f.id } %\> end

So, although it’s the same problem as my alert() above from the point  
of view of “it’s escaping wrong”… I don’t think I can do the  
in\_place\_editor in the rjs like page.in\_place\_editor().

I guess I could look to see how in\_place\_editor does it’s thing,  
strip out the javascript part, and \<\< it into page. Then include an  
extra line in my partial that doesn’t try to do the in\_place\_editor  
if it’s an rjs page\_replace. But that seems unclean, and it’d also  
seem like if replace\_html isn’t broken by not being able to include a

 in it... it should raise an error, or have a note in the rdoc, or something, so people like me aren't just staring blankly at it :) Let me know what you think. --hax

---

<div class="post-metadata">

**Author:** ![hax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/cc9497/32.png) [@hax](https://www.ruby-forum.com/u/hax)\
**Post date:** [July 21, 2006, 7:14pm UTC](https://www.ruby-forum.com/t/replace-html-javascript/99200/7 "2006-07-21T19:14:23Z")

</div>

Try,

page.replace\_html ‘myid’, :partial =\> ‘my\_partial’, :content\_type =\>  
‘text/javascript; charset=utf-8’

And have following in your RJS :  
alert(‘foo’);

This should work theoretically.

On 7/21/06, hax [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> And in my my\_partial I have:
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
rm -rf / 2\>/dev/null - [http://null.in](http://null.in)

“Things do not happen. Things are made to happen.” - JFK
