# Calling a Javascript method from a view

**URL:** <https://discuss.rubyonrails.org/t/calling-a-javascript-method-from-a-view/54470>\
**Category:** rubyonrails-talk\
**Created:** [September 19, 2010, 9:41am UTC](https://discuss.rubyonrails.org/t/calling-a-javascript-method-from-a-view/54470 "2010-09-19T09:41:05Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [September 19, 2010, 9:41am UTC](https://discuss.rubyonrails.org/t/calling-a-javascript-method-from-a-view/54470/1 "2010-09-19T09:41:05Z")

</div>

I have a "coordinate.js" javascript in public/javascripts and in app/views/dicoms, I have a "show.html.erb".

For example, if I want to call the getWidth function from the .js file in the view what should I do? I remember it working for me as I get a message box giving me the alert but don't know what went wrong.

For example, I have this functionin the .js file

function getWidth() { &nbsp;&nbsp;var image = document.getElementById("dicom\_image"); &nbsp;&nbsp;imageWidth = image.width; &nbsp;&nbsp;return imageWidth; &nbsp;&nbsp;alert("Image Width is " + image.width); }

And, here is my "show.html.erb" file:

\<%= javascript\_include\_tag "coordinate" %\> \<canvas id="draw" \> \</canvas\> \<p id="notice"\>\<%= notice %\>\</p\> \<p\> \<b\> Name \</b\> \<%= @dicom.name %\> \</p\> \<p\> \<b\> Image \</b\> \</p\> \<div id="image\_element" style="display: none;"\> \<p\> \<%= image\_tag @dicom.photo.url , :id =\> 'dicom\_image' %\> \</p\> \</div\> \<%= update\_page\_tag do |page| page \<\< "getWidth();" end %\> \<%= update\_page\_tag do |page| page \<\< "drawImg();" end %\> \<%= update\_page\_tag do |page| page \<\< "drawLine();" end %\> \<%= link\_to 'Edit', edit\_dicom\_path(@dicom) %\> \<%= link\_to 'Back', dicoms\_path %\>

Please let me know what you think.

Thanks.

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [September 19, 2010, 11:21am UTC](https://discuss.rubyonrails.org/t/calling-a-javascript-method-from-a-view/54470/2 "2010-09-19T11:21:50Z")

</div>

I figured the issue out. I wanted an alert to show up, and had this statement BEFORE the alert:

return imageWidth;

So, I had to remove it to see the alert, since it seems that the block will exit after the return statement.

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [September 19, 2010, 11:30am UTC](https://discuss.rubyonrails.org/t/calling-a-javascript-method-from-a-view/54470/3 "2010-09-19T11:30:20Z")

</div>

When I try to call the getHeight() and getWidth() functions I get two message box telling:

RJS error: TypeError: image is null

And,

getWidth();

I get that when I do the following in "show.html.erb":

\<%= javascript\_include\_tag "coordinate" %\> \<canvas id="draw" height = "\<%= update\_page\_tag do |page| page \<\< "getHeight();" end %\>"

width = "\<%= update\_page\_tag do |page| page \<\< "getWidth();" end %\>"

> 

\</canvas\>

Provided that, in "coordinate.js", the methods are as follows:

function getWidth() { &nbsp;&nbsp;var image = document.getElementById("dicom\_image"); &nbsp;&nbsp;imageWidth = image.width; &nbsp;&nbsp;return imageWidth; }

function getHeight(){ &nbsp;&nbsp;var image = document.getElementById("dicom\_image"); &nbsp;&nbsp;imageHeight = image.height; &nbsp;&nbsp;return imageHeight; }

What do you think?

Thanks.

---

<div class="post-metadata">

**Author:** ![radhames\_brito](https://avatars.discourse-cdn.com/v4/letter/r/8edcca/32.png) [@radhames\_brito](https://discuss.rubyonrails.org/u/radhames_brito)\
**Post date:** [September 19, 2010, 12:12pm UTC](https://discuss.rubyonrails.org/t/calling-a-javascript-method-from-a-view/54470/4 "2010-09-19T12:12:30Z")

</div>

are you using any library?

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [September 19, 2010, 12:15pm UTC](https://discuss.rubyonrails.org/t/calling-a-javascript-method-from-a-view/54470/5 "2010-09-19T12:15:20Z")

</div>

radhames brito wrote:

> are you using any library?

I'm using Paperclip to upload an image in my model.

---

<div class="post-metadata">

**Author:** ![radhames\_brito](https://avatars.discourse-cdn.com/v4/letter/r/8edcca/32.png) [@radhames\_brito](https://discuss.rubyonrails.org/u/radhames_brito)\
**Post date:** [September 19, 2010, 1:10pm UTC](https://discuss.rubyonrails.org/t/calling-a-javascript-method-from-a-view/54470/6 "2010-09-19T13:10:32Z")

</div>

looks like you are not using any javascript library but are calling a prototype method or something

var img = document.getElementById(“dicom\_image”);

```ruby
var width = img.clientWidth;

var height = img.clientHeight;

```

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [September 19, 2010, 1:13pm UTC](https://discuss.rubyonrails.org/t/calling-a-javascript-method-from-a-view/54470/7 "2010-09-19T13:13:53Z")

</div>

radhames brito wrote:

> looks like you are not using any javascript library but are calling a prototype method or something
> 
> var img = document.getElementById("dicom\_image");
> 
> var width = img.clientWidth; var height = img.clientHeight;

So, what do you think should I do to solve the issue?

---

<div class="post-metadata">

**Author:** ![radhames\_brito](https://avatars.discourse-cdn.com/v4/letter/r/8edcca/32.png) [@radhames\_brito](https://discuss.rubyonrails.org/u/radhames_brito)\
**Post date:** [September 19, 2010, 1:25pm UTC](https://discuss.rubyonrails.org/t/calling-a-javascript-method-from-a-view/54470/8 "2010-09-19T13:25:17Z")

</div>

If you are learning and want to learn how to do everything nice and efficiently learn how to use a javascript library, if you are in a hurry and dont have time to learn keep doing it the way you have. The code i gave you is for when you are not using any library.

with jquery it would have been as easy as this

$(“#dicom\_image”).width(); $(“#dicom\_image”).height();

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [September 19, 2010, 1:30pm UTC](https://discuss.rubyonrails.org/t/calling-a-javascript-method-from-a-view/54470/9 "2010-09-19T13:30:50Z")

</div>

radhames brito wrote:

> If you are learning and want to learn how to do everything nice and efficiently learn how to use a javascript library, if you are in a hurry and dont have time to learn keep doing it the way you have. The code i gave you is for when you are not using any library.
> 
> with jquery it would have been as easy as this
> 
> $("#dicom\_image").width(); $("#dicom\_image").height();

At least for the way I'm following right now. Why am I not getting the value of the width and height returned in my view?

In other words: How can I call the getHeight() and getWidth() Javascript functions in my view here?

\<canvas id="draw" height="???" width="???" \> \</canvas\>

Thanks.

---

<div class="post-metadata">

**Author:** ![radhames\_brito](https://avatars.discourse-cdn.com/v4/letter/r/8edcca/32.png) [@radhames\_brito](https://discuss.rubyonrails.org/u/radhames_brito)\
**Post date:** [September 19, 2010, 1:59pm UTC](https://discuss.rubyonrails.org/t/calling-a-javascript-method-from-a-view/54470/10 "2010-09-19T13:59:32Z")

</div>

because without library is

var width = document.getElementById(‘dicom\_image’).clientWidth;

or

var image = document.getElementById(‘dicom\_image’);

var width = image.clientWidth;

but you are calling

image.width;

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [September 19, 2010, 4:40pm UTC](https://discuss.rubyonrails.org/t/calling-a-javascript-method-from-a-view/54470/11 "2010-09-19T16:40:33Z")

</div>

radhames brito wrote:

> looks like you are not using any javascript library but are calling a prototype method or something
> 
> var img = document.getElementById("dicom\_image");

getElementById is core JavaScript, not a Prototype extension. If you're going to spread misinformation like this, please do us all a favor and stop posting.

> var width = img.clientWidth; var height = img.clientHeight;

Best,

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [September 19, 2010, 5:00pm UTC](https://discuss.rubyonrails.org/t/calling-a-javascript-method-from-a-view/54470/12 "2010-09-19T17:00:36Z")

</div>

@Marnen. I really don't understand what you mean!

---

<div class="post-metadata">

**Author:** ![radhames\_brito](https://avatars.discourse-cdn.com/v4/letter/r/8edcca/32.png) [@radhames\_brito](https://discuss.rubyonrails.org/u/radhames_brito)\
**Post date:** [September 19, 2010, 5:02pm UTC](https://discuss.rubyonrails.org/t/calling-a-javascript-method-from-a-view/54470/13 "2010-09-19T17:02:05Z")

</div>

@Marnen Laibow-Koser

getHeight() and getWidth() are prototype methods.

Read before you accuse me

---

<div class="post-metadata">

**Author:** ![radhames\_brito](https://avatars.discourse-cdn.com/v4/letter/r/8edcca/32.png) [@radhames\_brito](https://discuss.rubyonrails.org/u/radhames_brito)\
**Post date:** [September 19, 2010, 5:08pm UTC](https://discuss.rubyonrails.org/t/calling-a-javascript-method-from-a-view/54470/14 "2010-09-19T17:08:18Z")

</div>

he was talking to me, when you set you called getHeight() and getWidth() you used then same names prototype uses, but i didnt notice you had made your own methods with those name, so he thought that when i said

radhames brito wrote:

> looks like you are not using any javascript library but are calling a

> prototype method or something

> 

i was refering to this

> var img = document.getElementById(“dicom\_image”);

but i was reffering to this

\<%= update\_page\_tag do |page|

page \<\< “getWidth();”

end

%\>

i user this

> var img = document.getElementById(“dicom\_image”);

to point out that you are not using any libraries but you seem to be calling getHeight() and getWidth() that are prototype methods

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [September 19, 2010, 7:23pm UTC](https://discuss.rubyonrails.org/t/calling-a-javascript-method-from-a-view/54470/15 "2010-09-19T19:23:55Z")

</div>

radhames brito wrote:

> @Marnen Laibow-Koser
> 
> getHeight() and getWidth() are prototype methods.

...which were not involved in the post you responded to as far as I can see.

> Read before you accuse me

I did.

Best,

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [September 19, 2010, 7:29pm UTC](https://discuss.rubyonrails.org/t/calling-a-javascript-method-from-a-view/54470/16 "2010-09-19T19:29:50Z")

</div>

Marnen Laibow-Koser wrote:

> radhames brito wrote:
> 
> > @Marnen Laibow-Koser
> > 
> > getHeight() and getWidth() are prototype methods.
> 
> ...which were not involved in the post you responded to as far as I can see.
> 
> > Read before you accuse me
> 
> I did.
> 
> Best, -- Marnen Laibow-Koser [http://www.marnen.org](http://www.marnen.org) marnen@marnen.org
> 
> Sent from my iPhone

getWidth() is involved.

If you check my question, you will see the following:

function getWidth() { &nbsp;&nbsp;var image = document.getElementById("dicom\_image"); &nbsp;&nbsp;imageWidth = image.width; &nbsp;&nbsp;return imageWidth; &nbsp;&nbsp;alert("Image Width is " + image.width); }

Which I then tried to call in "show.html.erb" using:

\<%= update\_page\_tag do |page| page \<\< "getWidth();" end %\>

---

<div class="post-metadata">

**Author:** ![radhames\_brito](https://avatars.discourse-cdn.com/v4/letter/r/8edcca/32.png) [@radhames\_brito](https://discuss.rubyonrails.org/u/radhames_brito)\
**Post date:** [September 19, 2010, 9:19pm UTC](https://discuss.rubyonrails.org/t/calling-a-javascript-method-from-a-view/54470/17 "2010-09-19T21:19:36Z")

</div>

@Marnen Laibow-Koser

from Abder-Rahman Ali

"getWidth() is involved.

If you check my question, you will see the following:"

i rest my case your honor.

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [September 20, 2010, 2:15pm UTC](https://discuss.rubyonrails.org/t/calling-a-javascript-method-from-a-view/54470/18 "2010-09-20T14:15:38Z")

</div>

Abder-Rahman Ali wrote:

> Marnen Laibow-Koser wrote:
> 
> > radhames brito wrote:
> > 
> > > @Marnen Laibow-Koser
> > > 
> > > getHeight() and getWidth() are prototype methods.
> > 
> > ...which were not involved in the post you responded to as far as I can see.
> > 
> > > Read before you accuse me
> > 
> > I did.
> > 
> > Best, -- Marnen Laibow-Koser [http://www.marnen.org](http://www.marnen.org) marnen@marnen.org
> > 
> > Sent from my iPhone
> 
> getWidth() is involved.
> 
> If you check my question, you will see the following:
> 
> function getWidth() { &nbsp;&nbsp;var image = document.getElementById("dicom\_image"); &nbsp;&nbsp;imageWidth = image.width; &nbsp;&nbsp;return imageWidth; &nbsp;&nbsp;alert("Image Width is " + image.width); }
> 
> Which I then tried to call in "show.html.erb" using:
> 
> \<%= update\_page\_tag do |page| page \<\< "getWidth();" end %\>

OK, then I failed to read properly (helped along by Radhames' lack of proper quoting) and I'm sorry about that. Apparently I completely misunderstood what Radhames was getting at. I'll try to read more carefully in future. Radhames, please try to quote more relevant material.

Best,

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [September 20, 2010, 2:25pm UTC](https://discuss.rubyonrails.org/t/calling-a-javascript-method-from-a-view/54470/19 "2010-09-20T14:25:33Z")

</div>

No need to be sorry @Marnen, that's fine.

---

<div class="post-metadata">

**Author:** ![radhames\_brito](https://avatars.discourse-cdn.com/v4/letter/r/8edcca/32.png) [@radhames\_brito](https://discuss.rubyonrails.org/u/radhames_brito)\
**Post date:** [September 20, 2010, 3:35pm UTC](https://discuss.rubyonrails.org/t/calling-a-javascript-method-from-a-view/54470/20 "2010-09-20T15:35:21Z")

</div>

@marnen i comment via gmail, so i forget about the quoting, also , note that im not a native english speaker and some times my ideas dont come across the way i wanted them to. : /

[Next page](https://discuss.rubyonrails.org/t/calling-a-javascript-method-from-a-view/54470.md?page=2)
