Switching out JS variables

Jul 07, 2015

The code for certificate printing works great, but I think there is something I'm missing I thought it would be easy enough to just swap out an application variable for one of my user defined ones. More specifically the "g_oPrintOptions.strName" variable for my "learnername" variable that I have at the beginning of the course. My report file is attached the variables in question are at the very bottom. If anyone can explain why the code breaks when I swap the variables that would be helpful. I'm relatively green at JS but know HTML quite well. While I know JS in SL2 isn't officially supported I'm hoping someone with just a touch more JS knowledge can just tweak the format for me as im almost certain its a formatting error or a prefix I'm missing. 


                  Much appreciated 



12 Replies
James Murphy

The code for the certificate printing is at the end of the report.html file. It just changes the layout of the pre-packaged "print results" function in SL2 and adds a .jpg file to make it look official. The issue is that currently the learner puts in their name at the beginning of the course to customize the course with a variable i've called "learnername", then I simply recall %learnername% throughout the course to personalize. The certificate code in the report file attached above pulls the learners name from a box that pops up just prior to printing the certificate allowing the user to input their name that then appears on the certificate. I had originally thought that box defined the variable "g_oPrintOptions.strName" but I may be wrong, It just seems to be that one given its layout in the certificate in relation to the other information that populates. When its all said and done I want to use my "learnername" variable at the beginning of the course to populate the name on the certificate as opposed to the pop up box at the end so as to not have the learner have to input their name twice. The edited report file in my initial post produces the attached certificate just so it makes more sense. 

Norm Cousineau

Hi James. This is a bit tricky because I've never used that report function, and am not familiar with the flow you describe. But here's my thought: try to set the variable g_oPrintOptions.strName to the value of your defined learnername variable. For example, a trigger "When variable learnername changes, execute Javascript". The JS would be this:
g_oPrintOptions.strName = GetPlayer().GetVar("learnername");

(However, at that point the global variable g_oPrintOptions may not yet be defined, so it could cause a JS error. Check the browser's error console for this. If that's the case this solution won't work.)

Then you would not need the pop up window...can you prevent that? If you can't prevent it, then it may overwrite g_oPrintOptions.strName once it opens/closes.

Alternatively, you could edit the report.html file for example do this:

document.write("" + g_JamesMurphys_strName + "</br></br>");
(instead of g_oPrintOptions.strName)
And earlier do this:
g_JamesMurphys_strName = GetPlayer().GetVar("learnername");

That would never get overwritten...g_JamesMurphys_strName is a global variable you define that will not conflict with another global variable. So g_JamesMurphys_strName could be named something else, just as long as your confident it won't conflict with a SL defined variable.

The best way to define and refer to that variable would be using the 'window' prefix, making it a global variable... so this:

In other words DON'T use the 'var' keyword like so:
var g_JamesMurphys_strName = GetPlayer().GetVar("learnername");
(That would create a local variable, not accessible from report.html.)

James Murphy

This all makes a lot of sense and may be the best bunch of tips I've received yet. I can disable the pop up, when I do, the codes inability to find the pop up variable causes the code to halt that causes the problem. I feel that removing that variable and replacing it with the variables you've suggested here might do the trick. Thank you for your time in constructing the reply. Ill let you know how it goes today. 

            Thanks again


James Murphy

Thanks Norm ! Its only 4 lines that had to change that  I highlighted in the document attached. The script in the course seems to be working. but the report still only returns the JPG without the results on it. Im so close to finishing this I'm almost sure its my script syntax. perhaps somebody can take a look.

James Murphy

My thanks to Norm for going above and beyond here on the forums. He contacted me through my profile and a few emails later we had worked out where my code had gone wrong. I truly could not have done this without his help. The e-learning heroes on here are the best parts of choosing articulate.

      For anyone looking for a similar resolution find the attached code that I edited from the report file and images found HERE that works great with SL2. It helps to mention that the one part not in either of these documents is a small line of jS code that I had to run in the course from a trigger when the name variable box on the slide was updated. The code goes as follows

window.myGlobalVariable_strName = GetPlayer().GetVar("learnername");


Where "myGlobalVariable" you can see is an arbitrarily defined variable that matches the variable in the report file 

and "learnername" which is my text variable for the box within the SL2 course


Hope this give some insight into some of the other heroes out there 

Thanks again Norm (who really should get all the credit for this) 


This discussion is closed. You can start a new discussion or contact Articulate Support.