John Smith
John Smith

Reputation: 191

Google Chrome blocks ajax requests when print preview is opened on child window

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?

Upvotes: 19

Views: 8406

Answers (4)

DuckHunter
DuckHunter

Reputation: 91

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

Header add Access-Control-Allow-Origin "*"

Upvotes: 1

Syed Amir Bukhari
Syed Amir Bukhari

Reputation: 136

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;
}

Upvotes: 2

SlyBeaver
SlyBeaver

Reputation: 1312

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");

Upvotes: 1

dimkagfx
dimkagfx

Reputation: 126

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.

Upvotes: 0

Related Questions