I have this very basic quesiotn. I tired looking for the answear but so far all I found suggests that what I'm doing should be working and it isn't.
Using Snowman. When I define a function and invoke it in the same passage it works. When I define the function in the Story JavaScript I get an undefined error.
the example I'm using:
Story JavaScript:
function test() {
document.write('testing123');
};
Passage:
<% test() %>
Comments
One method used to make a Javascript variable or function (the same thing really) have global scope is to define it on the global window object like so. ... and you call the global function like so:
One problem with defining variables/function directly against the window object is you can accentually override existing variables/functions. You can use a namespace pattern to get a this issue, you add your functions to the namespace object, which in turn gets added to the window object. ... and you call the global namespace functions like so:
note: You can name your namespace whatever you like, just make sure whatever it is that it does not already exist on the window object.
Define: and call:
Any functions you add to the story.state variable may not survive the serialize/de-serialize process, which would result in them disappearing and producing undefined errors.