How to call a javascript function from rails view?


#1

Is there a way to call a javascript function from within a rails view?
I can’t use the onLoad javascript method inside the tag because I
don’t have control over the body tag, it’s in my header.
There are several rss feed reading functions that are in the page and I
would like to call those functions while the page is being loaded or
once it’s done loading. Currently everything works fine but I have to
click on several links to call those javascript functions which in turn
render a rss feed.
Is there a way to do this?
Maybe there is a way to manipulate the body tag somehow???
Thanks,
Mike M.


#2

Mike M. wrote:

Is there a way to call a javascript function from within a rails view?
I can’t use the onLoad javascript method inside the tag because I
don’t have control over the body tag, it’s in my header.
There are several rss feed reading functions that are in the page and I
would like to call those functions while the page is being loaded or
once it’s done loading. Currently everything works fine but I have to
click on several links to call those javascript functions which in turn
render a rss feed.
Is there a way to do this?
Maybe there is a way to manipulate the body tag somehow???
Thanks,
Mike M.

It’s alot simpler then you might expect.

Just put that where you’d want to call the function. I’ve used that
many times for calling scriptaculous effects, when I can’t use a
“:loading” or “:complete”.

Hope that helps,

-Gregg


#3

or with javascript tag helper

<%= javascript_tag “myFunc(‘foo’)” %>

or with tag helper and rjs

<%= javascript_tag render(:update) {|page| page.my_func ‘foo’} %>


#4

Mike,
You can just write your javascript in the rails view I think. When the
browser loads the page that you’ve used rails to generate it will
proccess
the javascript code as it normally would.

Here’s a simple example:

javascript alert

This will pop up an alert when you load this page.

You could just drop the code above onto your rails view page and
surround it
with standard

Not exactly pretty but it should work… you could also just put in a
function call there if you want.

If you really want to call your function in the onLoad event that’s
fired
then try the function below… (not mine…see url below)

function addLoadEvent(func) {
var oldonload = window.onload ;
if (typeof window.onload != ‘function’) {
window.onload = func;
} else {
window.onload = function() {
oldonload();
func();
}
}
}

addLoadEvent(nameOfSomeFunctionToRunOnPageLoad);
addLoadEvent(function() {
/* more code to run on page load */
});

I found this code here:
http://simon.incutio.com/archive/2004/05/26/addLoadEvent

Hope this helps
-Brian


#5

have a look at the prototype.js which is provided with rails.

Here is an answer which isn’t quite RJS (I haven’t look into RJS yet)
but
still it would work if you had prototype.js included in the header:


#6

Thank a lot to everyone for their suggestions.
I actually used the @content_for_script tag in the head section of my
header and then I use it to write some javascript functions to it. It
works fine.
Mike

Brian Ã?gren wrote:

have a look at the prototype.js which is provided with rails.

Here is an answer which isn’t quite RJS (I haven’t look into RJS yet)
but
still it would work if you had prototype.js included in the header: