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:

<p id="notice"><%= notice %></p>

<p> <b> Name </b> <%= @dicom.name %> </p> <p> <b> Image </b> </p> <p> <%= image_tag @dicom.photo.url , :id => 'coordinate' %> </p> <%= 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 Parkin'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?

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

Frederick Cheung 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. <body onload> is risky as well as not being unobtrusive.

Fred

Best,

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://jqueryui.com/

http://mootools.net/

http://www.dojotoolkit.org/

Thanks a lot everyone.