JavaScript redirecting away from course
Nov 28, 2013
I have a separate .js function running the code below, all the code works and the results are tracked, however the browser then redirects to the URL used in the post code. I want the code to execute in the background and the learners stays on the screen.
Any idea's of how to stop the course from redirecting, is it possible to add code so that as soon as the code is finished, stop?
function clickme() {
var player = GetPlayer();
var email = player.GetVar("email");
var firstname = player.GetVar("firstname");
var lastname = player.GetVar("lastname");
var country = player.GetVar("country");
var courseid = player.GetVar("courseid");
var division = player.GetVar("division");
var sHTML = "";
sHTML += "<form id='formScore' method='post' action=' http://URL/sendreport.aspx?USER_EMAIL=" + email + "&FIRST_NAME=" + firstname + "&LAST_NAME=" + lastname + "&COUNTRY=" + country + "&COURSE_ID=" + courseid + "&DIVISION=" + division + "'>";
sHTML += "<input type='hidden' id='USER_EMAIL' name='USER_EMAIL' value= " + email + ">";
sHTML += "<input type='hidden' id='FIRST_NAME' name='FIRST_NAME' value= " + firstname + ">";
sHTML += "<input type='hidden' id='LAST_NAME' name='LAST_NAME' value= " + lastname + ">";
sHTML += "<input type='hidden' id='COUNTRY' name='COUNTRY' value= " + country + ">";
sHTML += "<input type='hidden' id='COURSE_ID' name='COURSE_ID' value= " + courseid + ">";
sHTML += "<input type='hidden' id='DIVISION' name='DIVISION' value= " + division + ">";
sHTML += "<br><input type='submit'><br>";
sHTML += "</form>";
document.getElementById("divEmail").innerHTML = sHTML;
document.getElementById("formScore").submit();
alert('sent');
}
Thanks
Stephen
3 Replies
A couple of ways to silently submit forms. One is using ajax. I prefer to stay away from this method if cross domain is an issue, but it's one option. The other is to use a hidden iframe. Try this after your sHTML +=""; line. I didn't test this so it could require some tweaking...
ifrm = document.createElement("IFRAME");
document.body.appendChild(ifrm);
ifrm.width=ifrm.height=0;
ifrm.innerHTML=sHTML;
if (ifrm.contentWindow){
ifrm = ifrm.contentWindow;
}else{
if (ifrm.contentDocument && ifrm.contentDocument.document){
ifrm = ifrm.contentDocument.document;
}else{
ifrm = ifrm.contentDocument;
}
}
var newDiv=document.createElement("div");
newDiv.id="divMail"
ifrm.document.open();
ifrm.document.appendChild(newDiv);
ifrm.document.getElementById("divEmail").innerHTML = sHTML;
ifrm.document.getElementById("formScore").submit();
ifrm.document.close();
Thanks Steve for helping out Stephen here!
Hi Steve,
Thanks for taking the time to look at the problem, I'm going to test out the code you provided and see if I can get it to work.
I'm not a coder so I'm a bit out my depth with JavaScript.
Thanks again.
Stephen
This discussion is closed. You can start a new discussion or contact Articulate Support.