# \[ANN\] IFRAME based RJS - responds\_to\_parent

**URL:** <https://www.ruby-forum.com/t/ann-iframe-based-rjs-responds-to-parent/61916>\
**Category:** Rails\
**Created:** [May 29, 2006, 6:14pm UTC](https://www.ruby-forum.com/t/ann-iframe-based-rjs-responds-to-parent/61916 "2006-05-29T18:14:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sean\_Treadway](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/977dab/32.png) [@Sean\_Treadway](https://www.ruby-forum.com/u/Sean_Treadway)\
**Post date:** [May 29, 2006, 6:14pm UTC](https://www.ruby-forum.com/t/ann-iframe-based-rjs-responds-to-parent/61916/1 "2006-05-29T18:14:11Z")

</div>

\*\* File uploads with AJAX mojo \*\*

Respond with RJS to your parent window with a form action targeted to  
a hidden IFRAME. Handles all the painful situations like scoping  
your JavaScript to your parent window generating the script block for  
execution and clearing the IFRAME after execution so the back button  
doesn’t re-execute the action.

`plugin install http://sean.treadway.info/svn/plugins/ responds_to_parent`

class EnterpriseElevatorPitchExampleController \< ActionController::Base  
def iframe\_target\_action  
responds\_to\_parent do  
render :update do |page|  
page.hide :progress\_bar  
page.show :success  
page.replace\_html :success, ‘Finished’  
page.alert “RJS isn’t limited to XMLHttpRequest”  
end  
end  
end  
end

Discuss, comment, and feedback for improvement:  
[http://sean.treadway.info/articles/2006/05/29/iframe-remoting-made-easy](http://sean.treadway.info/articles/2006/05/29/iframe-remoting-made-easy)

# RespondsToParent

Adds responds\_to\_parent to your controller to respond to the parent  
document of your page.  
Make Ajaxy file uploads by posting the form to a hidden iframe, and  
respond with  
RJS to the parent window.

# Example

Controller:  
class Test \< ActionController::Base  
def main  
end

```
 def form_action
   # Do stuff with params[:uploaded_file]

   responds_to_parent do
     render :update do |page|
       page << "alert($('stuff').innerHTML)"
     end
   end
 end

```

end

main.rhtml:

Here is some stuff

---

<div class="post-metadata">

**Author:** ![Sean\_Treadway](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/977dab/32.png) [@Sean\_Treadway](https://www.ruby-forum.com/u/Sean_Treadway)\
**Post date:** [May 29, 2006, 10:53pm UTC](https://www.ruby-forum.com/t/ann-iframe-based-rjs-responds-to-parent/61916/2 "2006-05-29T22:53:51Z")

</div>

Published at [http://agilewebdevelopment.com/plugins/](http://agilewebdevelopment.com/plugins/)  
responds\_to\_parent 🙂

–  
Benjamin C.  
[http://www.bencurtis.com/](http://www.bencurtis.com/)  
[http://www.tesly.com/](http://www.tesly.com/) – Collaborative test case management  
[http://www.agilewebdevelopment.com/](http://www.agilewebdevelopment.com/) – Resources for the Rails community

---

<div class="post-metadata">

**Author:** ![Sean\_Treadway](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/977dab/32.png) [@Sean\_Treadway](https://www.ruby-forum.com/u/Sean_Treadway)\
**Post date:** [July 13, 2007, 5:09pm UTC](https://www.ruby-forum.com/t/ann-iframe-based-rjs-responds-to-parent/61916/3 "2007-07-13T17:09:19Z")

</div>

Sean Treadway wrote:

> \*\* File uploads with AJAX mojo \*\*
> 
> Respond with RJS to your parent window with a form action targeted to  
> a hidden IFRAME. Handles all the painful situations like scoping  
> your JavaScript to your parent window generating the script block for  
> execution and clearing the IFRAME after execution so the back button  
> doesn’t re-execute the action.
> 
> `plugin install http://sean.treadway.info/svn/plugins/ responds_to_parent`
> 
> class EnterpriseElevatorPitchExampleController \< ActionController::Base  
> def iframe\_target\_action  
> responds\_to\_parent do  
> render :update do |page|  
> page.hide :progress\_bar  
> page.show :success  
> page.replace\_html :success, ‘Finished’  
> page.alert “RJS isn’t limited to XMLHttpRequest”  
> end  
> end  
> end  
> end
> 
> Discuss, comment, and feedback for improvement:  
> [http://sean.treadway.info/articles/2006/05/29/iframe-remoting-made-easy](http://sean.treadway.info/articles/2006/05/29/iframe-remoting-made-easy)
> 
> # RespondsToParent
> 
> Adds responds\_to\_parent to your controller to respond to the parent  
> document of your page.  
> Make Ajaxy file uploads by posting the form to a hidden iframe, and  
> respond with  
> RJS to the parent window.
> 
> # Example
> 
> Controller:  
> class Test \< ActionController::Base  
> def main  
> end
> 
> ```
> def form_action
> # Do stuff with params[:uploaded_file]
> 
> responds_to_parent do
> render :update do |page|
> page << "alert($('stuff').innerHTML)"
> end
> end
> end
> 
> ```
> 
> end
> 
> main.rhtml:
> 
> Here is some stuff

Sean,  
Thanks for this excellent solution. I’ve successfully deployed this  
with my app.

But there is one small annoyance that I’ve encountered and was  
wondering if others are experiencing the same:

I’m running Firefox on Mac (and linux) with Firebug installed. After I  
upload an image, I get the following javascript error in Firebug:

#–  
loc is not defined  
[Break on this error] with(window.parent) { setTimeout(function() {  
window.eval('try {\nElemen…  
#–

I know it’s small, and does come with any side-effects, but still, it is  
annoying. Any ideas?

---

<div class="post-metadata">

**Author:** ![Sean\_Treadway](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/977dab/32.png) [@Sean\_Treadway](https://www.ruby-forum.com/u/Sean_Treadway)\
**Post date:** [March 9, 2008, 2:40am UTC](https://www.ruby-forum.com/t/ann-iframe-based-rjs-responds-to-parent/61916/4 "2008-03-09T02:40:19Z")

</div>

Elliott,

I had the same firebug error. For me the issue was that my response RJS  
was replacing the iframe I had posted to. Maybe you are having the same  
problem?

I am happy to help if you want to post your code or something…

Thanks  
-Jon
