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"> var next_week = 0; function testjs() { //next_week = next_week + 1; alert(''Next week'' + next-week); //return next_week; } </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. -- Posted via http://www.ruby-forum.com/.
On Jun 2, 2009, at 2:23 PM, Tyler Knappe wrote:> > 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"> > var next_week = 0; > function testjs() > { > //next_week = next_week + 1; > alert(''Next week'' + next-week); > //return next_week; > } > </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?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
Philip Hallstrom wrote:> On Jun 2, 2009, at 2:23 PM, Tyler Knappe 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. > > -philipI 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("next_week"); //alert(''test''); //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? -- Posted via http://www.ruby-forum.com/.
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
Tyler Knappe wrote:> Why is this the case? How could there be a syntax error?Does anyone know why this is throwing syntax errors? -- Posted via http://www.ruby-forum.com/.
2009/6/3 Tyler Knappe <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > Philip Hallstrom wrote: >> On Jun 2, 2009, at 2:23 PM, Tyler Knappe 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("next_week"); > //alert(''test''); > //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> -- > Posted via http://www.ruby-forum.com/. > > > >
Colin Law wrote:> 2009/6/3 Tyler Knappe <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>: > > 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. > > ColinHere 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. -- Posted via http://www.ruby-forum.com/.
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. -- Posted via http://www.ruby-forum.com/.
To include a javascript file of your own use <%= javascript_include_tag ''testjs'' %> where test.js is in public/javascripts Colin 2009/6/4 Mk 27 <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > 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. > -- > Posted via http://www.ruby-forum.com/. > > > >
2009/6/4 Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>:> 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> Colin > > 2009/6/4 Mk 27 <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>: >> >> 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. >> -- >> Posted via http://www.ruby-forum.com/. >> >> >> >> >
Colin Law wrote:> 2009/6/4 Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>: >> 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/javascriptsOkay, 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! :( -- Posted via http://www.ruby-forum.com/.
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. -- Posted via http://www.ruby-forum.com/.
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"> var i = 0; function testjs() { i = i + 1; //next_week = next_week + 1; //alert("next_week"); //alert(''test''); //return next_week; } </script> ---- vim app/views/layouts/labs.html.erb <%= javascript_include_tag :defaults %> <%= javascript_include_tag ''test'' %> ---- vim app/views/labs/show.html.erb <%= link_to_function ''Next Week Test'', "testjs()" %> ---- The errors: missing } in XML expression [Break on this error] i = i + 1;\n test.js?...244134114 (line 5) The second error, below, shows up when I click on the ''Next Week'' link within the show view. testjs is not defined [Break on this error] testjs(); I don''t see anything wrong, everything should be included. -- Posted via http://www.ruby-forum.com/.
On Jun 4, 5:53 pm, Tyler Knappe <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Here it is! Maybe something is formatted incorrectly then? > > vim public/javascripts/test.js >> <script type="text/javascript"> > var i = 0; > function testjs() > { > i = i + 1; > //next_week = next_week + 1; > //alert("next_week"); > //alert(''test''); > //return next_week; > } > </script>javascript files shouldn''t contain the <script> tags: they''re pure JS, not markup Fred
Frederick Cheung wrote:> On Jun 4, 5:53�pm, Tyler Knappe <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: >> >> Here it is! Maybe something is formatted incorrectly then? >> >> vim public/javascripts/test.js >> > >> </script> > javascript files shouldn''t contain the <script> tags: they''re pure > JS, not markup > > FredAha! Thank you Fred. This solved it. -- Posted via http://www.ruby-forum.com/.