最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google Chrome blocks ajax requests when print preview is opened on child window - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited Feb 11, 2013 at 13:50 Aaron Digulla 329k110 gold badges623 silver badges836 bronze badges asked Jan 22, 2013 at 15:32 John SmithJohn Smith 1911 silver badge3 bronze badges 7
  • 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 Commented Jan 22, 2013 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 Commented Jan 22, 2013 at 15:46
  • Ahh, I see. That sounds weird indeed. – Pekka Commented Jan 22, 2013 at 15:49
  • And Ajax requests are initiated on index to be more clear. – John Smith Commented Jan 22, 2013 at 15:50
  • 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 Commented May 1, 2014 at 16:00
 |  Show 2 more comments

4 Answers 4

Reset to default 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;
}

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

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

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

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.

发布评论

评论列表(0)

  1. 暂无评论