Getting js Variables from outside into Lectora variables

lbryan-4843
Community Member Posts: 12 ☆ Roadie ☆
I have a js code (code.js) that reads data and is read at Lectora via html Extension (js file). Then I try to pass it to Lectora but with no success via a run js action.
For example a variable in the code.js I have a:
function GetFirstName() {
return "a-name";
}
which gets the FirstName field from a database. Then in Lectora I tried a run js action of:
lectora_Fname=GetFirstName();
I do have a lectora_Fname variable in Lectora, but data does not pass. Seems that scrips does pass the value to lectora_Fname inside the js action (its own realm seems) but not to the Lectora's variable lectora_Fname (seems to be in Lectora real).
How can I get a js from outside Lectora to equal the one captured in the js code?
Any suggestions would be greatly appreciated...
Thanks,
Louie
For example a variable in the code.js I have a:
function GetFirstName() {
return "a-name";
}
which gets the FirstName field from a database. Then in Lectora I tried a run js action of:
lectora_Fname=GetFirstName();
I do have a lectora_Fname variable in Lectora, but data does not pass. Seems that scrips does pass the value to lectora_Fname inside the js action (its own realm seems) but not to the Lectora's variable lectora_Fname (seems to be in Lectora real).
How can I get a js from outside Lectora to equal the one captured in the js code?
Any suggestions would be greatly appreciated...
Thanks,
Louie
Tagged:
Comments
-
wheels Community Member, Administrator, Moderator, Rockstar Manager Posts: 685 eLearning ROCKSTAR Admin TeamYou need to use the JavaScript variable name, which you can find in the Variable Manager on the Tools menu.
In this case it would be Varlectora_Fname
To set this use the set function.Varlectora_Fname.set( GetFirstName() );
To get the value back use the getValue function.var fname = Varlectora_Fname.getValue();
Hope this helps. If you are finding that issues with your functions being called you might need to define all the functions, and call all the functions, from the Display Window.var win = GetDisplayWindow(); win.GetFirstName = function() { ... };
var win = GetDisplayWindow(); Varlectora_Fname.set( win.GetFirstName() );
-
CarlJFink Community Member, Lectora® Accessibility Group (LAUG) Member, eLearning Brothers® Partner Posts: 138 ♦ Idol ♦
-
Thanks Wheels...
It worked beautifully from the extern folder (js file) in the html extension to the run js action with the code you suggested...variables come in great-from SQL to the js file via a trigger for a WinSvc.
I also did the opposite (based on your great suggestion), from Lectora to a js file in another html extension which passes the information to a .txt file and from there a Data Load WinSvc takes the data into the SQL fields.
Basically this works like a mini-LMS locally in a tablet. The only thing that I have to figure out is how to get to advance to next page [ trivNextPage(); ] right after the Web app service is triggered it sort of stops to show the page creation and the Lectora next page does not work to move on. I tried an *.asgx and shows a blank page with no heading. In Lectora I move on in my title execution by manually clicking on the next page which is the html extension that executes the js code.
Anyway, thanks so much for your input...that's Guru type of answers!!! -
@lbryan-4843
Here is a link to a Knowledge Base article that expands on what @wheels showed in his great answer:
Containing your containers custom javascript suggestions -
One small problem has arise...while the extern LoadFromDB.js transfers the data from js variables to Lectora variables very nicely in "Preview" mode, when the title is published to offline the LoadFromDB.js is located in the same "Content" folder with the viewer and does not seem to pass the data. Even changing paths in a Sync Service.
I changed to using API and the API works (checked with Postman) getting the data from the SQLite but I tried to use same philosophy as with the LoadFromDB.js to not success.
The api is called in a js action with:
var FirstName;
function GetFirstName() {
let request = new XMLHttpRequest();
var uri = "http://localhost:5000/firstname/?tpatype=read&modid=1&lid=1"
request.open("GET", uri);
request.send();
request.onload = () => {
if (request.status === 200) {
FirstName = request.responseText;
//console.log(request.responseText);
}
else {
console.log(`error ${request.status} ${request.statusText}`)
}
}
}
and
in the HTML Extension I run:
varlectora_FName.set(GetFirstName());
and cannot get the value into the lectora_FName. I even tried to swap the codes, that is function into the HTML Extension and the bringing into Lectora [varlectora_FName.set(GetFirstName());] running in a js action, but no luck.
Are there limitations to using APIs in Lectora and why something would work in Preview and not offline?
My head now feels like an 8-ball.
Thanks,
Louie
Categories
- 35.9K All Categories
- 109 ✫ Announcements
- 33.1K Lectora®
- 31.1K Lectora Discussions
- 29K Lectora Desktop
- 2K Lectora Online
- 2K Lectora Feature Requests
- 71 Lectora User Groups
- 36 Lectora Accessibility User Group (LAUG)
- 25 ELB Learning Content
- 25 ELB Learning Content Discussions
- 328 CenarioVR®
- 195 CenarioVR Discussions
- 133 CenarioVR Feature Requests
- 42 Rockstar Learning Platform®
- 39 Rockstar Learning Platform Discussions
- 108 CourseMill®
- 108 CourseMill Discussions
- 47 ReviewLink®
- 47 ReviewLink Discussions
- 7 The Training Arcade®
- 7 The Training Arcade Discussions
- 936 All Things eLearning
- 37 eLearning Development
- 546 Learning Management System (LMS) Integration
- 333 Web Accessibility
- 1.2K ♪ The Green Room
- 9 Additional Learning Products