Returning values from javascript within a controller

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

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}

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