Mike Mikolajczyk
2006-May-02 18:46 UTC
[Rails] How to call a javascript function from rails view?
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 Mikolajczyk -- Posted via http://www.ruby-forum.com/.
Brian Chamberlain
2006-May-02 19:14 UTC
[Rails] How to call a javascript function from rails view?
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 On 5/2/06, Mike Mikolajczyk <mikem76@gmail.com> 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 Mikolajczyk > > -- > Posted via http://www.ruby-forum.com/ . > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060502/e7ce83c6/attachment.html
Brian Ă…gren
2006-May-02 20:04 UTC
[Rails] How to call a javascript function from rails view?
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> On 5/2/06, Brian Chamberlain <blchamberlain@gmail.com> wrote:> > 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 > > > > On 5/2/06, Mike Mikolajczyk < mikem76@gmail.com> 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 Mikolajczyk > > > > -- > > Posted via http://www.ruby-forum.com/ . > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060502/c030cbb2/attachment-0001.html
Gregg Pollack
2006-May-02 20:13 UTC
[Rails] Re: How to call a javascript function from rails view?
Mike Mikolajczyk 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 MikolajczykIt''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 -- Posted via http://www.ruby-forum.com/.
Alex Wayne
2006-May-03 01:16 UTC
[Rails] Re: How to call a javascript function from rails view?
or with javascript tag helper <%= javascript_tag "myFunc(''foo'')" %> or with tag helper and rjs <%= javascript_tag render(:update) {|page| page.my_func ''foo''} %> -- Posted via http://www.ruby-forum.com/.
Mike Mikolajczyk
2006-May-03 20:11 UTC
[Rails] Re: How to call a javascript function from rails view?
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>-- Posted via http://www.ruby-forum.com/.