# Returning values from javascript within a controller

**URL:** <https://www.ruby-forum.com/t/returning-values-from-javascript-within-a-controller/88133>\
**Category:** Rails\
**Created:** [March 8, 2007, 12:18pm UTC](https://www.ruby-forum.com/t/returning-values-from-javascript-within-a-controller/88133 "2007-03-08T12:18:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nathaniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ba8739/32.png) [@Nathaniel](https://www.ruby-forum.com/u/Nathaniel)\
**Post date:** [March 8, 2007, 12:18pm UTC](https://www.ruby-forum.com/t/returning-values-from-javascript-within-a-controller/88133/1 "2007-03-08T12:18:33Z")

</div>

Is there a way that I can get hold of the result of a javascript  
function call from within a controller?

There are a few instances where I want to do this. For example, suppose  
I would like to know if the page to be rendered is within a frame.

Within javascript I can determine if the page is within a frame by  
evaluating either of the conditions:

parent.frames.length != 0

or

parent.location && (parent.location != window.location)

I don’t know how to do this within Rails, and it would be useful to be  
able to render different content depending on this state.

Besides, for reasons too messy to explain, I use javascript to maintain  
a much more complicated “state” structure (that reacts to events and  
such) that is way too transitory to store within the database.

I would love to be able to query this state from within my controller.

Thanks, Nat

---

<div class="post-metadata">

**Author:** ![Nathaniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ba8739/32.png) [@Nathaniel](https://www.ruby-forum.com/u/Nathaniel)\
**Post date:** [March 8, 2007, 3:29pm UTC](https://www.ruby-forum.com/t/returning-values-from-javascript-within-a-controller/88133/2 "2007-03-08T15:29:09Z")

</div>

Rails can only get a hold of information that you send to it, so  
you’ll need to encode this state information on the query string and  
have your rails controllers handle it along with the rest of the  
request params.

In the case if your frames example, you’ll need to attach some sort of  
information on the query string to let it know that you’re loading the  
action up in a frame.

e.g.:

in your controller, get the state by decoding the junk you encoded:  
state = params[:state].split(’,’).map { |s| s.split(’=’) }.inject({})  
{|h,k| h[k.first] = k.last; h}

---

<div class="post-metadata">

**Author:** ![Nathaniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ba8739/32.png) [@Nathaniel](https://www.ruby-forum.com/u/Nathaniel)\
**Post date:** [March 11, 2007, 2:26am UTC](https://www.ruby-forum.com/t/returning-values-from-javascript-within-a-controller/88133/3 "2007-03-11T02:26:55Z")

</div>

Nathaniel wrote:

> Is there a way that I can get hold of the result of a javascript  
> function call from within a controller?
> 
> There are a few instances where I want to do this. For example, suppose  
> I would like to know if the page to be rendered is within a frame.
> 
> Within javascript I can determine if the page is within a frame by  
> evaluating either of the conditions:
> 
> parent.frames.length != 0
> 
> or
> 
> parent.location && (parent.location != window.location)
> 
> I don’t know how to do this within Rails, and it would be useful to be  
> able to render different content depending on this state.
> 
> Besides, for reasons too messy to explain, I use javascript to maintain  
> a much more complicated “state” structure (that reacts to events and  
> such) that is way too transitory to store within the database.
> 
> I would love to be able to query this state from within my controller.
> 
> Thanks, Nat

Here is suggestion.

Create a hidden field on the form and populate the field from java  
script result.

The field will be posted back to the controller as a parameter.

Hope this is of help.

Vin
