I'm looking to print the screen upon receiving an ajax response. Here's my code snippet:
function initiatePrint() {
var request = new XMLHttpRequest();
request .open("GET", "printPage.html", true);
var counter = 0;
request.onload = function() {
if (request.status >= 200 && request.status < 400){
console.log("success");
window.print();
} else {
// We reached our target server, but it returned an error
}
};
request.onerror = function() {
// There was a connection error of some sort
};
request.send();
}
However, I'm encountering an issue with an infinite loop on the response (seeing "success" multiple times in the console), almost as if window.print() is triggering the same ajax call again. What could be causing this and how can I successfully print the page?