6

I am trying to print an image from inside a div, the code runs ok but when the print dialog pops up, I would like the page to stay the same while actually it is showing only the picture as the window.reload doesn't work for some reasons. The javascript is telling me in console "Uncaught TypeError: window.reload is not a function"but i don't understand why.

  function printDiv(printableArea) {
    var printContents = document.getElementById("printableArea").innerHTML;
    var originalContents = document.body.innerHTML;
    document.body.innerHTML = printContents;
    window.print();
    window.close();
    window.reload();
  }

Click to print

<input type="button" onclick="printDiv('printableArea')" value="print a div!" />
4
  • Because there is no such method on window object. There is location.reload().
    – dfsq
    Dec 3, 2015 at 21:02
  • location.reload() might work
    – Aragorn
    Dec 3, 2015 at 21:02
  • 1
    Possible duplicate of How to reload a page using Javascript?
    – SeinopSys
    Dec 3, 2015 at 21:03
  • thanks, just tried window.location.reload(true); but yet the page prints the image itself without any style and the print dialog popsIn, then when i close the popUp the page reloads. i would like not to have that page
    – rob.m
    Dec 3, 2015 at 21:05

4 Answers 4

13

The reason you are getting the error is that there is no window.reload() method. You might want to use location.reload() to reload the page instead.

2
  • just tried, thanks. Yet when the dialog pops up, the document shows only the image and not the actual page. I thought the print dialog would keep the page that's why i am reloading. Now ok your answer is correct and I will accept it. Thakns
    – rob.m
    Dec 3, 2015 at 21:09
  • @rob.m could you clarify the issue you're having? Dec 3, 2015 at 21:11
3

Not sure if there's such function, Try location.reload()

details

0
1

use location.reload(); or window.location.reload(true);

https://webplatform.github.io/docs/apis/location/reload/

0

Use this to go to particular URL

window.location=url;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.