Onload and .html.erb

I have a Javascript file that finds the top-left coordinates of an
element (image) as follows:

function getTopLeftCoordinate() {
x=document.getElementById(‘coordinate’).offsetLeft
y=document.getElementById(‘coordinate’).offsetTop
alert (x)
alert (y)
}

And, on “show.html.erb”, I have the following:

<%= notice %>

Name <%= @dicom.name %>

Image

<%= image_tag @dicom.photo.url , :id => 'coordinate' %>

<%= javascript_include_tag "coordinate" %> <%= link_to 'Edit', edit_dicom_path(@dicom) %> <%= link_to 'Back', dicoms_path %>

As you know, if I want to run the Javascript code on loading the page, I
use <body onload=“getTopLeftCoordinate()”;>

How can I do that in my “show.html.erb” file? Since I don’t have a body
tag?

Thanks.

I used Tim P.'s post here: Render index view and RJS on load? - Rails - Ruby-Forum

Based on his solution, I had to do the following for it to work.

In “show.html.erb”:

<%= update_page_tag do |page|
page << “getTopLeftCoordinate();”
end
%>

Thanks.

what librery are you using?

radhames brito wrote:

what librery are you using?

What do you mean by the library here? I just used Javascript and
included the Javascript in the view (show.html.erb).

Does that make sense?

On Sep 12, 11:48 pm, Abder-Rahman A. [email protected] wrote:

As you know, if I want to run the Javascript code on loading the page, I
use <body onload=“getTopLeftCoordinate()”;>

How can I do that in my “show.html.erb” file? Since I don’t have a body
tag?

If I were you I’d have a separate js file with something like
document.observe(‘dom:loaded’, …) or $(document).ready that invokes
your javascript

Fred

You should use s javascript librery other wise it will take you ages to
make
something practical and even more time to make your code work on all
browsers.

javascript libreries lets you do thigns like client side validation,
date
picker, inline editing, autocomplete, slider, drag and drop, sorting
tooltips, progress bar
ajax. In other words make your site fell like a desktop application

http://mootools.net/

http://www.dojotoolkit.org/

On Mon, Sep 13, 2010 at 9:38 AM, Marnen Laibow-Koser

Frederick C. wrote:

On Sep 12, 11:48�pm, Abder-Rahman A. [email protected] wrote:

As you know, if I want to run the Javascript code on loading the page, I
use <body onload=“getTopLeftCoordinate()”;>

How can I do that in my “show.html.erb” file? Since I don’t have a body
tag?

If I were you I’d have a separate js file with something like
document.observe(‘dom:loaded’, …) or $(document).ready that invokes
your javascript

Agreed. is risky as well as not being unobtrusive.

Fred

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Thanks a lot everyone.