# Calling JavaScript

**URL:** <https://discuss.rubyonrails.org/t/calling-javascript/43124>\
**Category:** rubyonrails-talk\
**Created:** [June 2, 2009, 9:23pm UTC](https://discuss.rubyonrails.org/t/calling-javascript/43124 "2009-06-02T21:23:12Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [June 2, 2009, 9:23pm UTC](https://discuss.rubyonrails.org/t/calling-javascript/43124/1 "2009-06-02T21:23:12Z")

</div>

Hey All,

I'm trying to call a javascript function from my view:

\<%= link\_to\_function 'Next Week Test', "testjs()" %\>

Which \*should\* be calling this:

\<script type="text/javascript"\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var next\_week = 0; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;function testjs() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//next\_week = next\_week + 1; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert('Next week' + next-week); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//return next\_week; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} \</script\>

I've included /public/javascripts/test.js in my layout, it shows up as included in the source of the page:

\<script src="/javascripts/test.js?1243524559" type="text/javascript"\>\</script\>

However, it never seems to call the function resulting in the alert.

Anyone see something wrong?

Thanks.

---

<div class="post-metadata">

**Author:** ![Philip\_Hallstrom](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@Philip\_Hallstrom](https://discuss.rubyonrails.org/u/Philip_Hallstrom)\
**Post date:** [June 2, 2009, 9:36pm UTC](https://discuss.rubyonrails.org/t/calling-javascript/43124/2 "2009-06-02T21:36:51Z")

</div>

alert('Next week' + next-week); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^^^^^^^^^

That should be "next\_week".

Odds are you're getting a JS error, but not seeing it. Turn on whatever debugging you might have (firebug in firefox, or safari's debug inspector) and it will make this easy to catch.

-philip

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [June 3, 2009, 2:27pm UTC](https://discuss.rubyonrails.org/t/calling-javascript/43124/3 "2009-06-03T14:27:55Z")

</div>

Philip Hallstrom wrote:

> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var next\_week = 0;
> > 
> > \<script src="/javascripts/test.js?1243524559" type="text/javascript"\>\</script\>
> > 
> > However, it never seems to call the function resulting in the alert.
> > 
> > Anyone see something wrong?
> 
> &nbsp;&nbsp;alert('Next week' + next-week); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^^^^^^^^^
> 
> That should be "next\_week".
> 
> Odds are you're getting a JS error, but not seeing it. Turn on whatever debugging you might have (firebug in firefox, or safari's debug inspector) and it will make this easy to catch.
> 
> -philip

I fixed this and turned on firebug.

I am now seeing the following error:

missing } in XML expression [Break on this error] alert('test');\n

I tried just the following to simplify things:

\<script type="text/javascript"\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//var next\_week = 0; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;function testjs() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//next\_week = next\_week + 1; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//alert("next\_week"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//alert('test'); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//return next\_week; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//} \</script\>

Which calls an empty function.

Now I am seeing the following:

syntax error [Break on this error] }\n

Why is this the case? How could there be a syntax error?

---

<div class="post-metadata">

**Author:** ![glenn\_gillen](https://avatars.discourse-cdn.com/v4/letter/g/898d66/32.png) [@glenn\_gillen](https://discuss.rubyonrails.org/u/glenn_gillen)\
**Post date:** [June 4, 2009, 10:45am UTC](https://discuss.rubyonrails.org/t/calling-javascript/43124/4 "2009-06-04T10:45:49Z")

</div>

As a side note, link\_to\_function is the work of the devil. You should really be adding the listener unobtrusively using something like jquery/lowpro/prototype rather than defining it inline like that.

Glenn

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [June 4, 2009, 3:33pm UTC](https://discuss.rubyonrails.org/t/calling-javascript/43124/5 "2009-06-04T15:33:37Z")

</div>

> Philip Hallstrom wrote:
> 
> > > ```
> > > var next\_week = 0;
> > > 
> > > ```
> > > 
> > > \<script src="/javascripts/test.js?1243524559" type="text/javascript"\>\</script\>
> > > 
> > > However, it never seems to call the function resulting in the alert.
> > > 
> > > Anyone see something wrong?
> > 
> > alert('Next week' + next-week); ^^^^^^^^^
> > 
> > That should be "next\_week".
> > 
> > Odds are you're getting a JS error, but not seeing it. Turn on whatever debugging you might have (firebug in firefox, or safari's debug inspector) and it will make this easy to catch.
> > 
> > -philip
> 
> I fixed this and turned on firebug.
> 
> I am now seeing the following error:
> 
> missing } in XML expression [Break on this error] alert('test');\n
> 
> I tried just the following to simplify things:
> 
> \<script type="text/javascript"\> //var next\_week = 0; function testjs() { }
> 
> ```
> //next\_week = next\_week \+ 1;
> //alert\(&quot;next\_week&quot;\);
> //alert\(&#39;test&#39;\);
> //return next\_week;
> //\}
> 
> ```
> 
> \</script\>
> 
> Which calls an empty function.
> 
> Now I am seeing the following:
> 
> syntax error [Break on this error] }\n
> 
> Why is this the case? How could there be a syntax error?

I don't know why the syntax error, but if I had this problem I would keep deleting stuff till it goes away, then add it back to find exactly the problem. I have pasted your script into a page of mine and it does not give an error. Which line is the error reported on? Are you sure the script is not inside another tag that is confusing it? Try moving it out to the top level. Keep trying things till the error goes away then work out what causes it.

Colin

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [June 4, 2009, 3:53pm UTC](https://discuss.rubyonrails.org/t/calling-javascript/43124/6 "2009-06-04T15:53:23Z")

</div>

Colin Law wrote:

> I don't know why the syntax error, but if I had this problem I would keep deleting stuff till it goes away, then add it back to find exactly the problem. I have pasted your script into a page of mine and it does not give an error. Which line is the error reported on? Are you sure the script is not inside another tag that is confusing it? Try moving it out to the top level. Keep trying things till the error goes away then work out what causes it.
> 
> Colin

Here is something interesting:

When I click on the link I see this error in firebug:

testjs is not defined onclick(click clientX=71, clientY=479)HTsSTthn...RWQ%3D%3D (line 2) [Break on this error] testjs();

Could it be that test.js is not being included? I see this when checking the source:

\<script src="/javascripts/test.js?1244041352" type="text/javascript"\>\</script\>

So I would think that the function 'testjs' defined in test.js would be defined. I tried moving the function into application.js with the same result.

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [June 4, 2009, 4:17pm UTC](https://discuss.rubyonrails.org/t/calling-javascript/43124/7 "2009-06-04T16:17:12Z")

</div>

Tyler Knappe wrote:

> So I would think that the function 'testjs' defined in test.js would be defined. I tried moving the function into application.js with the same result.

and you have

\<%= javascript\_include\_tag :defaults %\>

in the header?

ps. IMO rails will be a very awkward place to learn javascript, if that is what you are doing.

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [June 4, 2009, 4:22pm UTC](https://discuss.rubyonrails.org/t/calling-javascript/43124/8 "2009-06-04T16:22:14Z")

</div>

To include a javascript file of your own use \<%= javascript\_include\_tag 'testjs' %\> where test.js is in public/javascripts

Colin

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [June 4, 2009, 4:24pm UTC](https://discuss.rubyonrails.org/t/calling-javascript/43124/9 "2009-06-04T16:24:16Z")

</div>

> To include a javascript file of your own use \<%= javascript\_include\_tag 'testjs' %\> where test.js is in public/javascripts

Correction \<%= javascript\_include\_tag 'test' %\> where test.js is in public/javascripts

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [June 4, 2009, 4:37pm UTC](https://discuss.rubyonrails.org/t/calling-javascript/43124/10 "2009-06-04T16:37:10Z")

</div>

Colin Law wrote:

> > To include a javascript file of your own use \<%= javascript\_include\_tag 'testjs' %\> where test.js is in public/javascripts
> 
> Correction \<%= javascript\_include\_tag 'test' %\> where test.js is in public/javascripts

Okay, I went and changed it to this:

\<%= javascript\_include\_tag :defaults %\> \<%= javascript\_include\_tag 'test' %\>

Where I am now including 'test' rather than 'testjs'. I changed this in my ~/app/view/layouts/[controller name].html.erb file.

I'm still getting the same error.

My idea was to not really use JS at all, because of the headache it seems to always be, but here I am! ☹

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [June 4, 2009, 4:53pm UTC](https://discuss.rubyonrails.org/t/calling-javascript/43124/11 "2009-06-04T16:53:33Z")

</div>

Mk 27 wrote:

> Tyler Knappe wrote:
> 
> > \<%= javascript\_include\_tag :defaults %\> \<%= javascript\_include\_tag 'test' %\>
> 
> If testjs() is in application.js or public/javascript/test.js it should respond if it is formatted correctly.

Here it is! Maybe something is formatted incorrectly then?

vim public/javascripts/test.js

\<script type="text/javascript"\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var i = 0; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;function testjs() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i = i + 1; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//next\_week = next\_week + 1; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//alert("next\_week"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//alert('test'); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//return next\_week; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} \</script\>

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [June 4, 2009, 5:21pm UTC](https://discuss.rubyonrails.org/t/calling-javascript/43124/12 "2009-06-04T17:21:06Z")

</div>

javascript files shouldn't contain the \<script\> tags: they're pure JS, not markup

Fred

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [June 4, 2009, 7:25pm UTC](https://discuss.rubyonrails.org/t/calling-javascript/43124/13 "2009-06-04T19:25:03Z")

</div>

Actually I wanted to thank everyone for helping me out. I know it wasn't a very fun experience, but it sure was enlightening for me. 🙂
