Forum Discussion
Problems with a javascript
function findLMSAPI(win) {
if (win.hasOwnProperty("GetStudentID")) return win;
else if (win.parent == win) return null;
else return findLMSAPI(win.parent);
}
var lmsAPI = findLMSAPI(this);
var newName = "Invitado";
if (lmsAPI) {
var myName = lmsAPI.GetStudentName();
var array = myName.split(',');
var firstName = array[1];
var lastName = array[0];
}
var player = GetPlayer();
player.SetVar("Nombre", firstName);
player.SetVar("Apellido", lastName);
- WaltHamiltonSuper Hero
Do you want to separate the variables in JavaScript, or in StoryLine?
When you refer to the variable as "working", what do you want it to do, and what does it do instead?
What are the other triggers that don't work? By "pass to", do you mean to jump to other slides, or pass variable contents to other slides?
If you can define these questions, and attach your .story here, it will be much easier for someone to help you.
- XavierToyosCommunity Member
Thank you Walt for your interest
Do you want to separate the variables in JavaScript, or in StoryLine?
When setting up a story to share with you I realized that Javascript does not separate the first name from the last name, it only stores it (the whole name) in the "Last name" variable. How can I separate name from surname?
When you refer to the variable as "working", what do you want it to do, and what does it do instead?
That returns the value of the variable ("Name"; "Last name" of the person logged into the LMS.
What are the other triggers that don't work? By "pass to", do you mean to jump to other slides, or pass variable contents to other slides?
In the story I share it works without problems. In the original, when Javascript is active, the triggers to move to the questions do not work. When Javascript is disabled it works smoothly (you can access the questions).
If you can define these questions, and attach your .story here, it will be much easier for someone to help you.
I thought that sharing the story didn't make sense, since the problem is related to the LMS and I can't test it with the same LMS as me. I have also had to reduce it as much as possible, it contains many graphs, animations, GIFS and copyrighted elements that I cannot share. I leave you a Story reduced to the maximum
Thank you for your interest and help
- PhilMayorSuper Hero
It works fine for me in scorm cloud, there is no linking of variables in Storyline I suspect the issue is elsewhere.
Do you have other javascript in the course? If there is an error in the other code it can stop all code being run. Have you checked the console for errors?
- XavierToyosCommunity Member
Thank you Phil
I had never used this code to extract the variables in our LMS. There are a lot of bugs in the console. I assume that our LMS provider must have a firewall to prevent attacks on the database and stops the execution of all the code.
I'll still use the method of asking for the name and saving it in a variable to address the user.
It has been an attempt with something to learn.
Thank you
- PhilMayorSuper Hero
In your previous post you suggested it has worked, however you have to use the variables in tandem. What are the errors you are seeing? There would be no reason why they would have to be used together.
Are you using any other unrelated code, that’s where the error maybe.
Sent from my iPhone
- KarlManning-f1eCommunity Member
Hi,
If it's not separating the names, it might be because of this line
var array = myName.split(',');You may have names separated by a space or other piece of punctuation, not a comma.
Also within Moodle, you may be storing the names in the reverse order.Add
var myName = lmsAPI.GetStudentName();
alert(myName);to see what the actual value is you are getting from Moodle.
Karl
- XavierToyosCommunity Member
- KarlManning-f1eCommunity Member
Hi,
You have 3 names on the screenshot, the code is only using 2. Also Moodle is storing them the other way round.
Try this:var array = myName.split(' ');
firstName = array[0];
lastName = array[1];
With your login lastName = TOYOS
If you want to use the last name from Moodle, allowing for 3 or 4 names, try this insteadlastName = array[array.length - 1];
With your login lastName would be RIERA
Hope that helps!
Karl - XavierToyosCommunity Member
Solved. The final solution is.....
function findLMSAPI(win) {
if (win.hasOwnProperty("GetStudentID")) return win;
else if (win.parent == win) return null;
else return findLMSAPI(win.parent);
}
var lmsAPI = findLMSAPI(this);
var newName = "Invitado";
if (lmsAPI) {
var myName = lmsAPI.GetStudentName();
var array = myName.split(' ');
var firstName = array[0];
var lastName = array[1];
}
var player = GetPlayer();
player.SetVar("Nombre", firstName);
player.SetVar("Apellido", lastName);.... works perfectly.
Thank you all