2 ms·
The JavaScript function "window.print()" is expected to open a print dialog for the current document. Since "window" is also the global object for the main/UI t
by masswerk 7y ago
The JavaScript function "window.print()" is expected to open a print dialog for the current document. Since "window" is also the global object for the main/UI thread in a browser, "print()" resolves to "window.print()", if no function has been defined otherwise.
This is, BTW, also a potential issue in Emscripten, which uses a similar logging mechanism by a custom "print()" function. (I guess, this is also, where it came from in the WASM implementation. A few years ago, I actually replaced all references to "print" by a more safe name for a customized Emscripten runtime, in order to prevent similar from happening in any edge cases. I suggest to treat "print" and similar global function names just as if they were reserved words in your projects to avoid "expectable unexpecteds" like this. For similar reasons, using a variable "self" to store a reference to the current this-object isn't a great idea, since "self" is already the canonical reference to the global object…)
Edit: To be more specific, "window" is only the global object for the scripting context in a browser window (anything inside "script" tags). Meaning, you would have to use the full form, "window.print()", when calling the print function from outside this context, like inside an event handler attribute of an HTML tag. (E.g., '<button onclick="window.print()">Print page<button>'. Here, a simple "print()" wouldn't work.)