# Calling/Executing javascript functions from controller

**URL:** <https://www.ruby-forum.com/t/calling-executing-javascript-functions-from-controller/101251>\
**Category:** Rails\
**Created:** [August 3, 2006, 8:03pm UTC](https://www.ruby-forum.com/t/calling-executing-javascript-functions-from-controller/101251 "2006-08-03T20:03:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![russ](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e36b37/32.png) [@russ](https://www.ruby-forum.com/u/russ)\
**Post date:** [August 3, 2006, 8:03pm UTC](https://www.ruby-forum.com/t/calling-executing-javascript-functions-from-controller/101251/1 "2006-08-03T20:03:55Z")

</div>

Hey all,  
How can I call a javascript function from my controller?  
Is it possible to simply call a javascript function  
from directly in the controller WITHOUT needing an AJAX  
request to come through via link\_to\_remote or remote form?

As an example I’ve tried:

class RandomController \< ApplicationController  
before\_filter :login\_required, :except =\> [:index, :initialize]  
def index  
@session[:mode] = ‘view’  
@map = GMap.new(“map”)

render :update do |page|  
page.call(“resizeMap”)  
end

@map.control\_init(:large\_map =\> true, :map\_type =\> true)  
@map.center\_zoom\_init([38.539506, -121.757172],13)  
store\_location  
end  
end

This as expected does not work, it spits of the try{blah blah} catch  
stuff.  
Any help is greatly appreciated!

Russ

---

<div class="post-metadata">

**Author:** ![russ](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e36b37/32.png) [@russ](https://www.ruby-forum.com/u/russ)\
**Post date:** [August 3, 2006, 8:18pm UTC](https://www.ruby-forum.com/t/calling-executing-javascript-functions-from-controller/101251/2 "2006-08-03T20:18:37Z")

</div>

Is the “resizeMap” function in the controller helper? If so try:

render :update do |page|  
page.resizeMap  
end

---

<div class="post-metadata">

**Author:** ![russ](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e36b37/32.png) [@russ](https://www.ruby-forum.com/u/russ)\
**Post date:** [August 3, 2006, 8:21pm UTC](https://www.ruby-forum.com/t/calling-executing-javascript-functions-from-controller/101251/3 "2006-08-03T20:21:16Z")

</div>

Greg wrote:

> Is the “resizeMap” function in the controller helper? If so try:
> 
> render :update do |page|  
> page.resizeMap  
> end

Take a look here:

[http://lists.rubyonrails.org/pipermail/rails/2006-April/029919.html](http://lists.rubyonrails.org/pipermail/rails/2006-April/029919.html)

… or Google “inline RJS”.

---

<div class="post-metadata">

**Author:** ![russ](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e36b37/32.png) [@russ](https://www.ruby-forum.com/u/russ)\
**Post date:** [August 4, 2006, 9:55pm UTC](https://www.ruby-forum.com/t/calling-executing-javascript-functions-from-controller/101251/4 "2006-08-04T21:55:09Z")

</div>

Greg,  
Thanks for the suggestion - however I have tried this before and had the  
following problem:

resizeMap() is a function in mapscript.js (an external js file) that is  
in  
the public/javascripts folder and included in the generated html page.

The code uses the Ym4r plugin for Google Maps/Yahoo Maps  
in my controller i have:

class RandomController \< ApplicationController  
before\_filter :login\_required, :except =\> [:index, :initialize]  
def index  
@session[:mode] = ‘view’  
@map = GMap.new(“map”)

```
render :update do |page|
  page.call("resizeMap")
end

@map.control_init(:large_map => true, :map_type => true)
@map.center_zoom_init([38.539506, -121.757172],13)
store_location

```

end  
end

But I end up with this as the output for the generated index.html:

try {  
resizeMap();  
} catch (e) { alert(‘RJS error:\n\n’ + e.toString());  
alert(‘resizeMap();’); throw e }

How can I actually call my javascript function and not end up with this?

Thanks,  
Russ
