# Calling a javascript function from rjs?

**URL:** <https://www.ruby-forum.com/t/calling-a-javascript-function-from-rjs/138240>\
**Category:** Rails\
**Created:** [May 21, 2008, 9:26am UTC](https://www.ruby-forum.com/t/calling-a-javascript-function-from-rjs/138240 "2008-05-21T09:26:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![suneeta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b77776/32.png) [@suneeta](https://www.ruby-forum.com/u/suneeta)\
**Post date:** [May 21, 2008, 9:26am UTC](https://www.ruby-forum.com/t/calling-a-javascript-function-from-rjs/138240/1 "2008-05-21T09:26:31Z")

</div>

Hi

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

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

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

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

Thanks  
Suneeta

---

<div class="post-metadata">

**Author:** ![suneeta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b77776/32.png) [@suneeta](https://www.ruby-forum.com/u/suneeta)\
**Post date:** [May 21, 2008, 9:44am UTC](https://www.ruby-forum.com/t/calling-a-javascript-function-from-rjs/138240/2 "2008-05-21T09:44:41Z")

</div>

On 21 May 2008, at 08:26, Suneeta Km wrote:

> ```
> {
> 
> ```
> 
> 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:** ![suneeta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b77776/32.png) [@suneeta](https://www.ruby-forum.com/u/suneeta)\
**Post date:** [May 21, 2008, 10:34am UTC](https://www.ruby-forum.com/t/calling-a-javascript-function-from-rjs/138240/3 "2008-05-21T10:34:27Z")

</div>

Frederick C. wrote:

> On 21 May 2008, at 08:26, Suneeta Km wrote:
> 
> > ```
> > {
> > 
> > ```
> > 
> > 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

Thank you Fred. Can you please give another solution to this problem?  
I have to replace a div if that div exists. So first I have to check the  
existence of div.

---

<div class="post-metadata">

**Author:** ![suneeta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b77776/32.png) [@suneeta](https://www.ruby-forum.com/u/suneeta)\
**Post date:** [May 21, 2008, 10:49am UTC](https://www.ruby-forum.com/t/calling-a-javascript-function-from-rjs/138240/4 "2008-05-21T10:49:29Z")

</div>

On 21 May 2008, at 09:34, Suneeta Km wrote:

> > 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
> 
> Thank you Fred. Can you please give another solution to this problem?  
> I have to replace a div if that div exists. So first I have to check  
> the  
> existence of div.

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

Fred

---

<div class="post-metadata">

**Author:** ![suneeta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b77776/32.png) [@suneeta](https://www.ruby-forum.com/u/suneeta)\
**Post date:** [May 21, 2008, 10:50am UTC](https://www.ruby-forum.com/t/calling-a-javascript-function-from-rjs/138240/5 "2008-05-21T10:50:29Z")

</div>

Suneeta Km wrote:

> Frederick C. wrote:
> 
> > On 21 May 2008, at 08:26, Suneeta Km wrote:
> > 
> > > ```
> > > {
> > > 
> > > ```
> > > 
> > > 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
