Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript window.open returns undefined

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?

like image 623
user3885331 Avatar asked Jan 11 '23 03:01

user3885331


1 Answers

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

like image 134
Reed Spool Avatar answered Jan 21 '23 22:01

Reed Spool