Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript window.print() not recognizing window object

Tags:

javascript

I am trying to provide a button to open a browser level print dialog on a window. I first tried window.print(); with an inline "onclick" on an <input> and then a click() function on the input using jQuery, and both give the same error when clicked:

TypeError: Property 'print' of object [object global] is not a function

I should point out that this is a popup window, but I wouldn't have thought that would matter except that any form of using window.print() on the parent page works fine.

Seemed like something must be happening to the window object somewhere, so I did the following in console:

window.name

"JOIN"

window.self

Window {top: Window, window: Window, location: Location, external: Object, chrome: Object…}

window.location

Location {assign: function, replace: function, reload: function, ancestorOrigins: DOMStringList, origin: "http://local.xxx.xxx:8080"…}

So it SEEMS like the window object is there and defined as expected.

I can even run other methods like close(), confirm(), alert(), scrollTo(), etc on this same window object and they work fine. So why wouldn't print()?

The contents of the page don't seem to matter, and I will also say we are not using iFrames or frames. I did replace ALL the content of the popup with just

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head></head>
<body>Foo</body>
</html>

with the same results. So I'm fairly certain none of our other code is interfering.

EDIT

The code that I was using to trigger print():

$('.foobar').click(function(){
    window.print();
});

tho now I'm just doing it in console.

The button

<input type="button" class="foobar" value="Print" />
like image 451
ambrojio Avatar asked Dec 08 '22 18:12

ambrojio


1 Answers

This would happen if you have an element with an id of print.

Element IDs become properties of the window (the global object), hiding any existing members with the same name

like image 195
SLaks Avatar answered Dec 11 '22 09:12

SLaks