Explore the community Forums Lectora Inspire Tools Questions & Answers How to get Variables from external JS? Reply To: How to get Variables from external JS?

#371574 Score: 0
Profile photo of Darrel Somoza
Darrel Somoza
friend finder
junior moderator
wise owl
curious george
209 pts

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.

  1. example.zip