admin管理员组

文章数量:1334194

I have a Rails 3.2 site and am using a javascript print routine that has suddenly stopped working. Here's my print routine code:

function print() {
    var mywin = window.open('', '', '');
    mywin.document.write('<body><link href="/assets/application.css" media="all"    rel="stylesheet" type="text/css" />' + document.getElementsByClassName('fields')[0].innerHTML + '</body>');
    mywin.print();
    mywin.close();
    return false;
}

Code has been running fine for months, but now whenever you try to print it just prints two blank pages. It throws an error on the second line, cannot read property 'document' of undefined, and inspection reveals that mywin is undefined.

Googling didn't yield any worthwhile results, so anyone have any clue why this is happening?

I have a Rails 3.2 site and am using a javascript print routine that has suddenly stopped working. Here's my print routine code:

function print() {
    var mywin = window.open('', '', '');
    mywin.document.write('<body><link href="/assets/application.css" media="all"    rel="stylesheet" type="text/css" />' + document.getElementsByClassName('fields')[0].innerHTML + '</body>');
    mywin.print();
    mywin.close();
    return false;
}

Code has been running fine for months, but now whenever you try to print it just prints two blank pages. It throws an error on the second line, cannot read property 'document' of undefined, and inspection reveals that mywin is undefined.

Googling didn't yield any worthwhile results, so anyone have any clue why this is happening?

Share Improve this question asked Jul 28, 2014 at 18:39 user3885331user3885331 411 gold badge1 silver badge2 bronze badges 2
  • 1 Where is this code being called from? A click event? Or programmatically? And what browser is this occurring in? – Ian Commented Jul 28, 2014 at 18:43
  • From a click event: <a href="#" onclick="print();return false;">Print</a>. Using Chrome. – user3885331 Commented Jul 28, 2014 at 19:48
Add a ment  | 

3 Answers 3

Reset to default 3

I had the same problem in a non-rails environment. I am using node's http-server, working on localhost.

The trouble was that Chrome was automatically blocking pop-ups from localhost. I went to the settings and added "localhost" as an exception, and now everything works great!

Hope this helps

It has been a few years since I first posted an answer. Coming back with a fresh pair of eyes always helps.


Since you want to open a window and dynamically create the HTML document, the URL matters. Apparently a blank URL causes window.open(...) to return an undefined value, rather than throw an exception. Instead, specify about:blank as the URL:

function print() {
    var mywin = window.open('about:blank', '', '');
    //                       ^^^^^^^^^^^

    mywin.document.write('<body><link href="/assets/application.css" media="all"    rel="stylesheet" type="text/css" />' + document.getElementsByClassName('fields')[0].innerHTML + '</body>');
    mywin.print();
    mywin.close();
    return false;
}

If that doesn't work, consider placing a blank HTML file on the web server and loading that URL. As long as it es from the same domain, JavaScript can call document.write(...) in that new window.

A related problem and the answer:

This code results in win being undefined

let win = window.open('https://www.xerox.', '_blank').focus(); 

The problem is that the .focus() function returns undefined

Working code:

let win = window.open('https://www.xerox.', '_blank');
win.focus(); 

本文标签: ruby on railsJavascript windowopen returns undefinedStack Overflow