I have a Captivate user variable called Q1Answer on a slide.
I have this js inside a Captivate 2019 HTML5 Object on that slide:
var ANSWER = "Q1Answer";
var answer = parent.window.cpAPIInterface.getVariableValue(ANSWER);
This works perfectly during the initial visit to the slide.
Upon revisit to the slide, however, it crashes when it tries to retrieve the value for ANSWER.
I get this at var answer = parent.window.cpAPIInterface.getVariableValue(ANSWER);
TypeError: Cannot read property 'window' of null
at Array.<anonymous> (MGCFQ1-response.html:375)
at cp.EventEmitterClass.trigger (eval at e (CPXHRLoader.js:37), <anonymous>:1051:250)
at b.EventManager.fireEvent (eval at e (CPXHRLoader.js:37), <anonymous>:470:28)
at window.svvi (eval at e (CPXHRLoader.js:37), <anonymous>:508:166)
at setQ1Answer (eval at window.DefineProperty (eval at e (CPXHRLoader.js:37)), <anonymous>:1:83)
at eval (eval at executeAction (eval at e (CPXHRLoader.js:37)), <anonymous>:1:16)
at cp.Movie.executeAction (eval at e (CPXHRLoader.js:37), <anonymous>:996:432)
at Function.b.clickSuccessHandler (eval at e (CPXHRLoader.js:37), <anonymous>:921:426)
at Function.b.clickHandler (eval at e (CPXHRLoader.js:37), <anonymous>:920:167)
at b.ch (eval at e (CPXHRLoader.js:37), <anonymous>:920:225)
Any ideas on how to fix this?
I believe the problem is when you enter the slide your web object sets up. When you exit the slide, while the web object is 'deleted', the code it initialized still remains. So when the CPAPI_VARIABLEVALUECHANGED event listener is triggered again, the code will try to access the 'parent' object and discover it is now empty.
When you move back into the slide the Web Object initializes again, so now you have two instances of the code running. To get around this, you probably need to remove the CPAPI_VARIABLEVALUECHANGED event listener when you exit the slide.
You might also try saving parent.window to a variable outside the event handler functio, and then access it within the Event Handler function. This will allow you to have access to the Captivate window object even though the Web Object's 'parent' property is now unavailable. That said, if there is any chance that your course will run on Internet Explorer, IE will not be happy with this as it has more rigid security protocols in this area. For this kind of matter, I'd regularly test in Internet Explorer.
Your assessment of the issue is correct. I should have mentioned that I already tried removing the listener as described in your first suggestion. That didn't work.
You make a great point about IE. Probably best to take an entirely different approach and avoid using the event listener in the first place.