Topic: How to run Javascript onload?

How do I run Javascript in the controller, as onload?

Re: How to run Javascript onload?

If you want javascript to run when the page loads the simplest way is to modify your  /app/views/layouts/application.rhtml like so:

<body<%= " onload='#{@onload}'"%>>

And put something like this in your controller:
  def index
    @onload = 'window.alert("hello")'
  end

That's not very clean code - but it should work.

Re: How to run Javascript onload?

danger wrote:

If you want javascript to run when the page loads the simplest way is to modify your  /app/views/layouts/application.rhtml like so:

<body<%= " onload='#{@onload}'"%>>

And put something like this in your controller:
  def index
    @onload = 'window.alert("hello")'
  end

That's not very clean code - but it should work.

Ok. Thanks. Then I might use the "regular" way with window.onload in the main Javascript. My aim is to affect css, and I guess it can only be done by Javascript

Re: How to run Javascript onload?

If you don't want to write javascript, define your function with thr Prototype URL helpers:

<script type="text/javascript" language="javascript">
    // <![CDATA[
    function onload() {
        <%= remote_function :url => { options } %>       
    };
    // ]]>
</script>
<body onload="onload()">

Re: How to run Javascript onload?

Seems like having a function called onload() is causing me problems. I renamed it to on_load() and all is ok:

<script type="text/javascript" language="javascript">
    // <![CDATA[
    function on_load() {
        <%= remote_function :url => { options } %>       
    };
    // ]]>
</script>
<body onload="on_load()">

Re: How to run Javascript onload?

danger, I liked your answer, but I wasn't happy with the onload event being in the controller, so I moved it to the view.

application.html.erb:

  <body <%= content_for?(:body_attributes) ? yield(:body_attributes) : "" %> > 

index.html.erb:

<%= content_for :body_attributes, "onload='my_onload_function();'" %>

<script type="text/javascript">
  function my_onload_function()
  {
  }
</script>
 

Last edited by Gib (2011-08-22 02:40:45)

Re: How to run Javascript onload?

Some times you don't want the users to select the ckeckbox. You can do this by Disabling the check box. Disabling a checkbox means that the checkbox is grayed out .

To disable a checkbox, add the line DISABLED to the <type=

Re: How to run Javascript onload?

Some times you don't want the users to select the ckeckbox. You can do this by Disabling the check box. Disabling a checkbox means that the checkbox is grayed out .
http://www.webdevelopmentseo.com/
To disable a checkbox, add the line DISABLED to the <type="checkbox" name="c1" value="check me 1" > tag so the final result looks like .

The following example shows how to lock and unlock the check box on some conditions.You can do this by calling a javascript function on onclick() event.
Lock checkbox2
Lock checkbox1
Unlock all checkboxes
Copy and paste the following code.

<form name=exf1>
<input type="checkbox" name="c2" value="check me 2" onclick=doIt(2)> Lock checkbox2
<input type=hidden name=h2 value=0>
<input type="checkbox" name="c3" value="check me 3" onclick=doIt(3)> Lock checkbox1
<input type=hidden name=h3 value=0>
<input type="checkbox" name="c1" value="check me 1" onclick=doIt(1)> Unlock all checkboxes
<input type=hidden name=h1 value=0>
</form>
<script language=JavaScript> var U=0;L=1; // (U)nlocked & (L)ocked
function doIt(_v)
{
if(eval("document.exf1.c"+_v+".checked"))
{
if(_v==1){unlock(2);unlock(3);}
if(_v==2){lock(3);}
if(_v==3){lock(2);}
}
else
{
if(_v==1){whipe(2);whipe(3);unlock(2);unlock(3);}
if(_v==2){unlock(3);}
if(_v==3){unlock(2);}
}
}
function lock(_v)
{
eval("document.exf1.c"+_v+".disabled=true"); // IE thing
eval("document.exf1.h"+_v+".value=L");
}
function unlock(_v)
{
eval("document.exf1.c"+_v+".disabled=false"); // IE thing
eval("document.exf1.h"+_v+".value=U");
}
function whipe(_v)
{
eval("document.exf1.c"+_v+".checked=false");
}

</script