I want to add data to the current webpage with jQuery before I print the webpage. The reason is that I have a digital CV on my website and don't want to have my personal information on the website available to crawlers etc. I created an XHR request to get my personal information before printing and want to inject the data into HTML. I found this script to find out with jQuery when a user is printing. This works fine in general but the problem is that the script works "too slow", meaning that the data will not be appended before the print dialogue pops up. This is my HTML code:
<div>
<span id="cvnameplaceholder">My Name</span><br />
<span id="cvstreetplaceholder"></span><br />
<span id="cvcityplaceholder"></span><br /><br />
<span id="cvemailplaceholder"></span>
</div>
And this is the jQuery code which returns an array like {street: 'ABC', city: 'DEF', email: 'mail@example.com}
$(document).ready(function() {
var beforePrint = function() {
$.post("./api/RequestHandler.php", {request : "getCVInformation"}, function (response) {
response = JSON.parse(response);
$('#cvstreetplaceholder').text(response.street);
$('#cvcityplaceholder').text(response.city);
$('#cvemailplaceholder').text(response.email);
})
};
if (window.matchMedia) {
var mediaQueryList = window.matchMedia('print');
mediaQueryList.addListener(function(mql) {
if (mql.matches) {
beforePrint();
}
});
}
window.onbeforeprint = beforePrint;
}());
When I hit the print button a second time, the information is there. So is there any possibility to add some text before showing the actual print dialogue?