# calling a javascript function from rjs?

**URL:** <https://discuss.rubyonrails.org/t/calling-a-javascript-function-from-rjs/29629>\
**Category:** rubyonrails-talk\
**Created:** [May 21, 2008, 7:26am UTC](https://discuss.rubyonrails.org/t/calling-a-javascript-function-from-rjs/29629 "2008-05-21T07:26:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [May 21, 2008, 7:26am UTC](https://discuss.rubyonrails.org/t/calling-a-javascript-function-from-rjs/29629/1 "2008-05-21T07:26:31Z")

</div>

Hi

&nbsp;&nbsp;From an rjs file, I called a javascript function in ci.js as follows: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;page.call 'test',params[:id]+'parents'

&nbsp;&nbsp;In ci.js I wrote the following: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;function test(div\_name) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert("testing........" +div\_name) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ($(div\_name)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t=1; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return t; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t=0; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return t; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;Now in the rjs file I have to get the value of t which is returned from the function test in ci.js.

&nbsp;&nbsp;How can I get the value of t? Please help.

Thanks Suneeta

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [May 21, 2008, 7:44am UTC](https://discuss.rubyonrails.org/t/calling-a-javascript-function-from-rjs/29629/2 "2008-05-21T07:44:00Z")

</div>

> Hi
> 
> From an rjs file, I called a javascript function in ci.js as follows: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;page.call 'test',params[:id]+'parents'
> 
> In ci.js I wrote the following: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;function test(div\_name) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert("testing........" +div\_name) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ($(div\_name)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t=1; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return t; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t=0; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return t; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;}
> 
> Now in the rjs file I have to get the value of t which is returned from the function test in ci.js.

You can't. the rjs file is evaluated server side, but the javascript  
is executed on the client at a later state (except of course in the  
sense that your rjs can assign the result of that function call to  
another javascript variable, pass it as an argument to another  
javacsript function etc..., but there's still the server/client  
boundary that can't be crossed).

Fred

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [May 21, 2008, 8:34am UTC](https://discuss.rubyonrails.org/t/calling-a-javascript-function-from-rjs/29629/3 "2008-05-21T08:34:27Z")

</div>

Frederick Cheung wrote:

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [May 21, 2008, 8:48am UTC](https://discuss.rubyonrails.org/t/calling-a-javascript-function-from-rjs/29629/4 "2008-05-21T08:48:43Z")

</div>

Other than what I said when you asked yesterday, no.

Fred

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [May 21, 2008, 8:50am UTC](https://discuss.rubyonrails.org/t/calling-a-javascript-function-from-rjs/29629/5 "2008-05-21T08:50:29Z")

</div>

Suneeta Km wrote:

> Frederick Cheung wrote:
> 
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ Now in the rjs file I have to get the value of t which is returned from the function test in ci.js.
> > 
> > You can't. the rjs file is evaluated server side, but the javascript is executed on the client at a later state (except of course in the sense that your rjs can assign the result of that function call to another javascript variable, pass it as an argument to another javacsript function etc..., but there's still the server/client boundary that can't be crossed).
> > 
> > Fred

How can I call an action from the function test? Please help

Suneeta
