Forum: Ruby on Rails How to call a javascript function from rails view?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Mike M. (Guest)
on 2006-05-02 22:46
Is there a way to call a javascript function from within a rails view?
I can't use the onLoad javascript method inside the <body> 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.
Gregg P. (Guest)
on 2006-05-03 00:13
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 <body> 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.

<script type="text/javascript">
     myfunction();
</script>

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".

<script type="text/javascript">
     new Effect.Highlight('thisdiv');
</script>

Hope that helps,

-Gregg
Alex W. (Guest)
on 2006-05-03 05:16
or with javascript tag helper

<%= javascript_tag "myFunc('foo')" %>

or with tag helper and rjs

<%= javascript_tag render(:update) {|page| page.my_func 'foo'} %>
Brian Chamberlain (Guest)
on 2006-05-03 21:00
(Received via mailing list)
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:
<html>
<head><title>javascript alert</title></head>
<body>
<script language="javascript" type="text/javascript">alert('hello
all')</script>
</body>
</html>

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
<script language="javascript" type="text/javascript" >
YourRailsGeneratedJavascriptToBeExecuted
</script>

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
Brian Ã?gren (Guest)
on 2006-05-03 21:01
(Received via mailing list)
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:

<script language="JavaScript" type="text/javascript">
function doSomething()
{
   alert("Yeah, man!");
}
 Event.observe ( window, 'load', doSomething, false ); // <- notice the
LACK
of () on doSomething

</script>
Mike M. (Guest)
on 2006-05-04 00:11
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:
>
> <script language="JavaScript" type="text/javascript">
> function doSomething()
> {
>    alert("Yeah, man!");
> }
>  Event.observe ( window, 'load', doSomething, false ); // <- notice the
> LACK
> of () on doSomething
>
> </script>
This topic is locked and can not be replied to.