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!" />
rob.m
  • 9,843
  • 19
  • 73
  • 162
  • Because there is no such method on window object. There is `location.reload()`. – dfsq Dec 03 '15 at 21:02
  • location.reload() might work – Aragorn Dec 03 '15 at 21:02
  • 1
    Possible duplicate of [How to reload a page using Javascript?](http://stackoverflow.com/questions/3715047/how-to-reload-a-page-using-javascript) – SeinopSys Dec 03 '15 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 03 '15 at 21:05

4 Answers4

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.

Nathan Hinchey
  • 1,191
  • 9
  • 30
  • 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 03 '15 at 21:09
  • @rob.m could you clarify the issue you're having? – Nathan Hinchey Dec 03 '15 at 21:11
3

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

details

Aragorn
  • 5,021
  • 5
  • 26
  • 37
3

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

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

kangkyu
  • 5,252
  • 3
  • 34
  • 36
pedram amini
  • 121
  • 1
  • 1
  • 7
0

Use this to go to particular URL

window.location=url;
Tyler2P
  • 2,324
  • 26
  • 22
  • 31