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
3 Answers
Reset to default 3I 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
版权声明:本文标题:ruby on rails - Javascript window.open returns undefined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742312681a2451189.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论