I don't understand how the responseHandler function below is receiving a response from an API call, after a payment form submission. Here is a brief summary of the code:
var PaymentThing = function(e) {
this["clientCallback"] = e;
return {
createToken: function() {
// HTTP GET URL is generated and then an HTML script element is appended to the page head
var a = "https://example.com/securitytoken?creditcard=xxxx?callback=PaymentThing.callback";
var f = document.createElement("script");
f.src = a;
document.getElementsByTagName("head")[0].appendChild(f)
},
callback: function(e) {
this["clientCallback"](e.status, e.results)
}
}
}
var responseHandler = function(status, response) {
// How does this even work?
console.log(response, status);
}
// Form Submission
jQuery('#checkout_payment_form').submit(function() {
PaymentThing.createToken(responseHandler);
}
So my question is, how does the responseHandler function receive the response from the HTTP GET call? It looks to me like the HTTP call isn't even being made. I only see a URL being added to an HTML script element (with the createToken function), with the URL never even being called/executed (but somehow it is?). Also, how are the (status, response) parameters passed into the responseHandler function when I don't see them being passed into a responseHandler function call?
I don't understand the logic or order of operations here, can anyone please explain what's going on?
EDIT: Ok I think I see now how the responseHandler function is receiving the status and response parameters. I edited the code above to show there's a callback function in the generated HTTP url. But still, how is that URL being called? It still looks to me like it's inert and being stuck into the HTML of the page and not activating.
f.onload = arguments[0]or something like that?