Use System Print Dialog in Chrome in JavaScript - javascript

We have an issue where Chrome's "Print Preview" does not print our pages correctly. If you use the "Use System Print Dialog link" (Ctrl+Shift+P), it prints our page fine (almost identically to Firefox).
We have a button on our page that calls window.print() to open the print dialog. However, in Chrome it opens to the "Print Preview" dialog which ends up not printing our page correctly.
Is there away with JavaScript to print directly to the "system print dialog" in Chrome?
P.S.
I do know how the end user can disable the Print Preview in chrome://flags, but what I want to know is there anyway to prevent it from showing when I programmatically call window.print() (or similar) regardless of user settings.

Print dialogs are not scriptable using JavaScript. They're proprietary parts of browsers themselves.

Hi i am facing the same issue... I even tried firing keyboard event ctrl+shift+P from my script in order to show system print dialog.
You can fire a keyboard event and that issue is successfully dispatched, however the value of keyCode that the browser receives is always 0 (instead of ASCII value of ‘P’)
There is a bug logged https://bugs.webkit.org/show_bug.cgi?id=16735 against webkit for the same… and here are some posts http://code.google.com/p/chromium/issues/detail?id=27048, http://code.google.com/p/chromium/issues/detail?id=52408 mentioning the same issue in Safari and Chrome…

Related

How would you stop a page from refreshing automatically?

I have a problem when I am trying to check the source of an interesting page which keeps refreshing automatically every 3-5 seconds (presumably due to some js script) which resets my Inspect Element Inspector window every time the page is refreshed.
Is there any other way other to stop that page from refreshing or perhaps the Inspector window from resetting itself other than turning on NoScript to stop the page from refreshing automatically?
Usually I just open DevTools, switch to the appropriate panel if necessary, and hit pause.
Opening DevTools: Via menus, or by press F12, Ctrl+Shift+I, or Cmd+Shift+I depending on browser and OS.
Switching panels: Pick the panel from the tabs at the top of DevTools. It'll be called "Debugger" (Firefox, IE) or "Sources" (Chrome) or similar.
Pausing: In the Debugger/Sources panel, click the pause button (usually looks like the pause button on a television remote control, ||) or press the keyboard equivalent. Keyboard equivalents are
Firefox & Chrome: F8
IE: Ctrl+Shift+B
(Updated 2020-03-30)
In Firefox 74 this option is in Options -> Privacy & Security -> Permissions
(Original reply)
Firefox has the option to prevent refresh natively, the option is in Advanced->General->Warn me when websites try to redirect or reload the page
The most popular solution for this problem is to trap the beforeunload event. The browser will ask the user for confirmation to leave the page. The code, in its simplest form, looks like this:
window.onbeforeunload = function() { return true }
You can enter this code in console. Alternately, you can simply paste the following URL in the browser address bar (console not required). You can even bookmark it.
javascript:window.onbeforeunload = function() { return true }
Be advised that modern browsers might chop off the javascript: part when you paste it inside the address bar; make sure you type it back.
To determine the cause of redirect in Firefox, try the following:
Open Web Developer Tools (CTRL + SHIFT + I), open "Toolbox Options" and check the "Enable persistent logs" option. This makes the logs persist across page loads (logs are cleared otherwise).
Now switch to "Network Monitor" tab.
Open the URL and let it refresh.
Inside the Network Monitor > Cause column you will find out why the page reloads.
The cause column is pretty ambiguous (Chrome does a much better job). However, if JavaScript was used to trigger page (re)load then it at least shows you the filename and line number of that script.
When the page is still loading, you can press the Esc key. While the page is still white, press it. When you stop the page from loading at this point, this usually stops all the auto loaded javascript. Any scripts that run on actions are usually not effected. Each page is different, try different timings.
When I use a site called NovelUpdates there is javascript that can make certain elements hidden, and when I press Esc on page load all the elements that would be hidden after page load are visible. Then when I click a button that would execute javascript that operates with no problems. NoScript isn't going to solve your issue I believe.
Another example of this are those websites with annoying boxes that pop out after 10 seconds that says you aren't a member and can't view any more of this site without logging in, like some news article websites.
What you could do is use the command exit(), which is the equivalent to die in php and simply stops the script.
If you don't know what's causing it and you don't want to look for the "bad boy", then you might as well stop the entire script at the very bottom of the page.

Disable print preview in Chrome

Is there a way to disable the print preview in Chrome using javascript? I need to open a pop-up in a web application with some text to print; in this pop-up I have the following code when the page is loaded:
$(document).ready(function () {
window.print();
});
a simple JavaScript that opens the print preview page; now, what happens is that the print preview page behaves like a modal dialog: I cannot navigate anymore in the application that has opened the print page (even though the links are clickable). This behavior doesn't show up if the normal system dialog is available (and in fact, I don't have this problem in IE Explorer and in Firefox).
So, is there a way to tell Chrome to disable the print preview through a JavaScript?
Is there a way to disable the print preview in Chrome using javascript?
No (through javascript). Google like every other company, likes to keep a consistent user experience. If programmers were able to change how chrome worked on different pages beyond html, that would be a poor user experience.
now, what happens is that the print preview page behaves like a modal dialog: I cannot navigate anymore in the application that has opened the print page (even though the links are clickable).
Instead, you could have a new window open with the same data and hava javascript do a print. The previous window would work normally (assuming the new window doesn't open in a new tab because of add-ons/configuration).

Show PDF for Website Printing

Can you link printing a webpage to printing a PDF?
Ideally, the PDF would be downloaded once the user uses the browser's print functionality (so a performance hit doesn't affect users who are not printing the website).
I'm looking for a Javascript/CSS print media query solution.
To answer my own question:
You can trigger Javascript to run on a print event, redirecting the user to a PDF (works for IE 5+, Firefox 6+, Chrome 9+, and Safari 5+):
Detecting browser print event
Replace the console.logs in the code snippet from the above link with window.location = "http://whatever.you.want.com"
Using this method, the print dialog will open briefly, close immediately and redirect the user to a PDF where they will have to hit the print button again.

automatically press keyboard keys with jQuery

Here is a button whenever it's clicked. Automatically keybord keys are pressed ctrl+shift+p.
This is actually window print shortcut key.How can I do that. Because when window.print() function is used in google chrome. chrome show chrome print dialog box. but I want window print dialog and this is the shortcut key of window print dialog.
You can disable the default behavior of chrome print box. May be you can refer this link for that
http://ryan.mcgeary.org/2012/09/13/disable-chrome-print-dialog-use-osx-instead/
This will serve well for you but If you are looking something to do it globally for all the users then you may have to go through some other option... Will try to find an alternate for that as well.
Hope this be of some help
Happy Learning

.click not working in chrome

A sample of my code is here: http://develop.davzy.com/test2.php
<input name="avatarfile" type="file" id='uploadbutton' />
<script>document.getElementById("uploadbutton").click()</script>
It is supposed to open the file selector as soon as you open the page. It works in IE8, IE9, FireFox, but not in Chrome.
It won't work, not even in Firefox (7+). It's a security measure to avoid hijacking of the input field.
Regarding the pop up, Firefox warns you that the page is trying to pop up a window (the file upload dialog is an ui window) on page load, so the user is notified and is given the choice to allow it or block it.
Anyway, why would you need to open an file dialog on page load?
is because the browser blocks pop-ups, at least in firefox tells me a pop-up blocking

Categories