I have an RJS script that renders a partial with some javacript that
runs a calendar script. Basically:
var cal_#{id} = new CalendarPopup();
cal_#{id}.setReturnFunction(“setMultipleCalDateValues_#{id}”);
But this doesn’t seem to get executed within the browser. In fact, if I
view the “generated source” in Firefox the JS portion doesn’t even show
up. If I use “page << “[javascript source]”” it doesn’t work either.
If I render the partial from within a view it works fine. If I call the
RJS directly I can see the javascript within a try block.
RJS is Ruby code that generates Javascript. So just typing out straight
Javascript gets interpreted as Ruby, but if it’s just a string it gets
discarded as an unused local variable.
page << “javascript” is what you want. Can we see the entirety of the
RJS
file?
RJS is Ruby code that generates Javascript. So just typing out straight
Javascript gets interpreted as Ruby, but if it’s just a string it gets
discarded as an unused local variable.
page << “javascript” is what you want. Can we see the entirety of the
RJS
file?
The error I’m actually getting is that cal_[id] (the one that I’m
creating with ‘var cal_[id] = new CalendarPopup();’) is not defined when
I click on a image that references cal_[id] in it’s onClick. Is
variable created, but in a different context?
If I replace my javascript with “alert(“test”);” I get the alert. So
I think it’s trying to execute but is not for some reason. Since the
javascript works when called as a partial within a view, could it have
something to do with how it is serialized for the ajax response?
I don’t know what your general issue is, but ‘cal_[id]’ is not a valid
HTML id code. They can’t have a [] in them. That’s why Rails emits
tags with dual identifiers; a name like ‘cal_[id]’, and an id like
‘cal__id’.
I don’t know what your general issue is, but ‘cal_[id]’ is not a valid
HTML id code. They can’t have a [] in them. That’s why Rails emits
tags with dual identifiers; a name like ‘cal_[id]’, and an id like
‘cal__id’.
Doing some more testing I figured out than I can alert cal_[id] in the
javascript in the RJS file and it sees it as an object. So the variable
is getting defined, but when I reference it in an onClick on the page
it’s not defined. Which leads me to believe it’s getting
defined/executed in a different context than the main window.
It’s very possible. I can’t remember what scoping Javascript does for
different browsers, but your RJS code is executed as such:
try {
[ your code here ]
} catch {
alert("[your code here again]"); // in development mode
}
So you’re defining the variables in a try/catch block, which might be
causing scoping issues.
Jason
That’s it. I was using the var keyword when declaring cal_[id]. If I
remove var then I can access the variable in the rest of the page.
Thanks to everyone for helping me work through this.
Also for anyone else interested in this problem:
I was also defining a function by name “function fname_1 (){}” and I
could not access this function elsewhere in the document. I needed to
assign the function to a variable “fname_1 = function () {}”