19

There are 2 files: index.html and print.html

First one contains a button that opens print.html using simple command:

window.open("print.html", "_blank", "menubar=yes,toolbar=yes,status,scrollbars,resizable");

print.html contains only one button that opens print preview dialog:

<button onclick="window.print();">

The problem appears when print preview dialog is opened. In this case any action on index.html - i.e. the other file that initiate ajax request - is temporary blocked and put into queue. And only when preview is closed browser fires all requests.

I can see it only in Google Chrome (24.0.1312.52 m).

Can anybody confirm that this is Chrome's bug?

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
John Smith
  • 191
  • 1
  • 3
  • 1
    Correct me if I'm wrong, but this doesn't sound like a bug to me. Isn't the print preview a modal dialog? Why should Ajax requests fire while it is open? – Pekka Jan 22 '13 at 15:35
  • 2
    index.html and print.html are two different windows. index opened print.html using window.open.Print preview is opened on print.html. Why index.html is blocked? It's OK that print window is blocked. – John Smith Jan 22 '13 at 15:46
  • Ahh, I see. That sounds weird indeed. – Pekka Jan 22 '13 at 15:49
  • And Ajax requests are initiated on index to be more clear. – John Smith Jan 22 '13 at 15:50
  • I can't confirm it (never seen it but I rarely print) but I agree, it sounds like a bug. Just to be sure: Can you try to open two completely different web sites which do AJAX and see if Chrome also blocks when you open a print dialog for some other domain? – Aaron Digulla Feb 11 '13 at 13:52
  • 2
    I can confirm this on Chrome 34 on Windows 8, I've tried Canary build v26, and it works fine. And worse this, is that if the user actually use the close button of the window (and not the Cancel button of the print dialog), it keeps the print dialog open in the background or something and prevents any subsequent XHR requests. – jValdron May 01 '14 at 16:00
  • I can confirm this issue too - latest version of Chrome (34.0.1847.131 m) on Windows 7, closing the print-window with the 'X'-sign won't continue the execution of XHR requests. – stuXnet May 12 '14 at 09:26

4 Answers4

2

there is a Chrome bug where window.print() does not work when there is a tag in the DOM. It might be solved by calling this function:

function printPage() {
    window.print();

    //workaround for Chrome bug - https://code.google.com/p/chromium/issues/detail?id=141633
    if (window.stop) {
        location.reload(); //triggering unload (e.g. reloading the page) makes the print dialog appear
        window.stop(); //immediately stop reloading
    }
    return false;
}
Shubh
  • 6,693
  • 9
  • 48
  • 83
  • I am having a similar issue and this is occurring on Chrome Version 37.0.2062.103 m. Your solution worked only once. Retried with the same page instance and the print dialog did not open from the window.open'ed page. I bumped it up cause it was a good attempt and maybe can be made to work. – Josef.B Sep 05 '14 at 01:57
  • For those interested, [here](https://code.google.com/p/chromium/issues/detail?id=359627) is the filed bug... it says it was fixed in v34 and v35 of Chrome, but since then has reappeared. – incutonez Sep 25 '15 at 19:04
  • I am having the same issue but with 2 separate tabs (see http://stackoverflow.com/questions/42932835/angular-js-print-tab-blocks-http-request-google-chrome). How can I use this workaround in my case? – molerat Mar 21 '17 at 16:34
1

Your server not added ORIGIN headers. You need add it on .htaccess. For example:

Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"

Or you can add it in PHP on print.html (if you can use PHP on html files)

header ("Access-Control-Allow-Origin: *");
header ("Access-Control-Allow-Headers: origin, x-requested-with, content-type");
header ("Access-Control-Allow-Methods: PUT, GET, POST, DELETE, OPTIONS");
SlyBeaver
  • 1,272
  • 12
  • 22
1

You need to install mod_headers on Apache and set it on .htaccess

Header add Access-Control-Allow-Origin "*"
DuckHunter
  • 91
  • 1
  • 6
0

I had similar issue with Chrome - because of the security policy it can't access local files. When I am making an AJAX call I get this error

XMLHttpRequest cannot load file:///*. Origin null is not allowed by Access-Control-Allow-Origin.

From what I know - you should launch Chrome with params:

--allow-file-access-from-files

Hope it helps.

dimkagfx
  • 126
  • 1
  • 6
  • Having the same issue as John, I'm afraid to say that `--allow-file-access-from-files` doesn't solve it :/ – stuXnet May 12 '14 at 09:28