JavaScript
Jan 26, 2017
Could someone please share some JavaScript code that I can embed in my HTML to save the StoryLine 2 score to my database. I have my course developed. All I need now is to save the results to my database. KIS (Keep It Simple) would be great. QAD (Quick And Dirty) is OK as long as it works. I can always enhance it later.
Thanks,
Bill
5 Replies
OK, I see there are no takers on this one. Maybe I need to be a little more precise, so let me try again.
I already have the data I want in a user defined variable named "Score". How do I get it to my database?
Hey William - Although we don't support sending Storyline variables to a database, there is some examples in a previous discussion that may be helpful. Otherwise, hopefully someone in the community will be able to chime in and assist.
Leslie,
I would like to say thank you a million times. This info was posted a long time ago, and people are still asking for this info today. It would be nice to have this added as a StoryLine tutorial. Better yet, develop an interface tool that would guide us through interfacing with our local databases. This would make this product attractive to many more users. I don't know who to make this suggestion to But I think it is a good idea. On the other hand, it might be important to keep this info hard to get and keep this product hard to use in the interest of job security for the JavaScript elite. Just being cynical. I am very thankful to you and the other contributors that have shared this info. Thanks again a million times.
Best Regards,
Bill
(William CTR Rupert)
D10 CBI Distance Learning Programmer
Haha! We are not wanting to keep it hard to get, it's just a hard area to assist in when you technically do not support the JavaScript coding. I know it's out of my realm of knowledge for sure. I am glad that information in the other thread was helpful to you. You raise a valid point about a tool and you are welcome to share your thoughts with our product development team here.
I needed to do this last week and I was not able to get it to work based on discussion in that previous thread so I tried to do it on my own over the weekend. Here is the step by step in which I was able to post score back to my web server using javascript.
There are 3 components to this whole thing. I will try my best to make this sample as simple and as generic as I can.
So now in your .story file, you need to find the appropriate slide to put this javascript in. I'd put this in my result slides.
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
var userid = vars['userid'];
var player = GetPlayer();
var score = player.GetVar("ScorePercent"); // replace your variable here
var href = 'https://myWebsite.com/save_score/' + userid;
var iframe = document.createElement("iframe");
iframe.name = 'my_iframe';
iframe.style="width:0; height:0; border:0; border:none";
var form = document.createElement("form");
document.body.appendChild(iframe);
document.body.appendChild(form);
form.method = "POST";
form.action = href;
form.target = "my_iframe";
var element1 = document.createElement("INPUT");
element1.name="score";
element1.value = score; // save score here
element1.type = 'hidden';
form.appendChild(element1);
form.submit();
This discussion is closed. You can start a new discussion or contact Articulate Support.