How to get Variables from external JS?

I've been trying to connect my variables to a external js sheet, that'll change their value.

But everything i've tried so far, leaves the variable as doesn't exist, but it exist in the Lectora project.

Is there an additional step that needs to be done to get an external javascript to communicate with the course?

so far

VarHeader.set(value);

VarHeader.getValue();

returns nothing.

Discussion (5)

Yes, there is. You have to initialize the variable before it can be accessed in Lectora using JavaScript. There are many ways of doing this. As a quick test, on load of the page modify the variable by setting it to equal itself. You should then be able to access it with JS as you did above.

if the code is apart of the lectora export, it seems to work.

But I'm using an external js, so i can add edit to it without having to re-export. It doesn't seem to work. I keep getting it's not a function in the console.log

Is there some special lectora command that needs to be added to it to help make it work?

I know captivate needs a special command to get access to their program's function, maybe lectora does too?

Are you saying, I should have a action on the opening page that says something like

undefined VariableA = VariableA

If you post what you have I'll take a look at it.

undefined

Darrel

I think I understand what you want. Check out this example. In it I have added a link to an external script that I use to hold a variable. You can edit the external script and change the value of the variable without having to re-export the lesson.

click the top button and a display message will pop up with the current value of Lectora Variable1 (should be 0). Then click the bottom button to re-assign Variable1 to the value of myVar1 in the external JavaScript file. Clicking the top button will then show that Variable1 now equals the value from the external script. If you change the value of the external JS variable you will not have to re-export for the change to take effect (re-run the lesson to see the change). I hope I explained this sufficiently.

One thing to note. I used 'bottom of file scripting' to link to the external script. The Trivantis application developers have this incorrect as it places what you link in this way AFTER the closing HTML tag. The only reason why it works is because modern browsers do a very good job at working with broken, bad HMTL. Bottom of file scripting SHOULD put the script just BEFORE the closing BODY tag.

Also keep in mind that if you go this route, you'll have to manually place the external JS file in the right location in the exported files. My attachment includes the file main.js. This is the external JS file that I put there after the export.

Regarding what I posted earlier. Normally, you need to in some way use a Lectora variable (to initialize it) before you can use it with JS. You're onPageShow action would do the trick.

Discussions have been disabled for this post