0

problem about doing it online , it works fine in localhost

i have this code its prints the specific div by div id

    function printDiv(divID) {
        //Get the HTML of div
        var divElements = document.getElementById(divID).innerHTML;

        //Get the HTML of whole page
        var oldPage = document.body.innerHTML;

        //Reset the page's HTML with div's HTML only
        document.body.innerHTML ="<html><head><title> </title></head><body>"+divElements + "</body>";

        //Print Page
        window.print();

        //Restore orignal HTML
        document.body.innerHTML = oldPage;
}

it works beautiful in my localhost

but when i upload the file to my onlinehost it show like this picture

image shows the error

can someone help me what is the error ?

  • You want to set html and head part in body, really? – A. Wolff Nov 23 '15 at 13:35
  • to add some text for print page ,and not showing them in the main page – khaled el wakeel Nov 23 '15 at 13:41
  • Ya but BODY element cannot content HTML and HEAD elements, you would anyway duplicate these elements and of course this is invalid HTML markup. And the dupe link is exactly what you are trying to accomplish – A. Wolff Nov 23 '15 at 13:43
  • i will try moving html and head , and will let body only – khaled el wakeel Nov 23 '15 at 13:46
  • No remove body, it would be invalid too... You just need: `document.body.innerHTML = divElements;`. But be aware, reseting itto original content once printed, you'll lose all binded events/datas (if not delegated to document level). I'd instead just hide all elements without a specific class using e.g: `@media print {:not(.toPrint){display: none;}}`. And: `function printDiv(divID) {$('#' + divId).addClass('toPrint'); window.print(); $('#' + divId).removeClass('toPrint');}` – A. Wolff Nov 23 '15 at 13:48
  • ok thanks man , i will try doing it – khaled el wakeel Nov 23 '15 at 13:51
  • Forget about media print, i made mistake in rule – A. Wolff Nov 23 '15 at 13:57

0 Answers0