window.print() opens two print dialogs on FF and Chrome - javascript

Yes, as the name suggests, I am getting two print dialogs on Chrome latest and FF latest.
The code I'm using is:
var $printButton = $('#print_button');
if ($printButton) {
$printButton.on('click', function () {
window.print();
})
}
The second dialog only opens when I have actioned the first dialog (print or cancel), if that helps.
EDIT:
After narrowing it down to an issue with my callback, I put a console.log('hello'); in the script and found that it was calling twice on refresh.
My script file was being included twice somewhere on the page. Thank you legacy code :)
Thanks for the advice everyone.

Related

Javascript 'alert' function inside catch block only works when dev tools are opened in chrome

Hello StackOverflow community!
I've encountered a very weird problem and couldn't find any useful information on how to solve it.
Somehow, a piece of javascript code works only when the dev tools window is opened (docked or as a separate window) in google chrome.
The original problem: Due to our application structure, we need to open multiple popups automatically when a page is served. Since the popups are NOT opened through a direct user interaction (like onclick), modern browsers would automatically block these popups. Because of the large amount of code that would need to be refactored to avoid this, our solution was:
check if the browser is blocking some popups.
if so: inform the user about this and suggest to turn off their browser's popup blocking function for
our website (by adding it to the exception list for example).
Not a very elegant solution I know, but there was no other way so please don't comment on how to do this differently.
The javascript code:
let popupBlockingErrorShown = false;
this.OpenWindow = function (url, name, args) {
var i = Popups.length; //Popups is an array defined as a global variable that keeps track of all
//opened popup windows
Popups[i] = window.open(url, name, args);
try {
Popups[i].focus();
} catch (e) {
if (!popupBlockingErrorShown) {
alert("very user friendly message explaining to turn of popup blocking");
popupBlockingErrorShown = true;
}
};
}
The windows have to be popups. The popupBlockingErrorShown variable is to prevent having an alert message for each popup.
Works fine in firefox. But in google chrome there is this behaviour:
without dev tools open: the first popup opens normally, the others are blocked, there is no alert message.
with dev tools open: the first popup opens but gets 'stuck' on loading (it's an empty page). The alert message shows normally.
Keeping the browser-window open and simply switching between dev tools opened or closed gives the same behaviour.
Anyone can help me? Much appreciated!
This is my first stackoverflow question and I'm still very new to programming, I have a bit over a year of experience. Remarks on my 'asking questions'-skills are welcome.
Ok thanks to wOxxOm's comment I've found a workaround. So the problem was related to what window was focused on. I've added a piece of code in the catch-block to show an alert on a successfully opened popup (if there is one) :
try {
Popups[i].focus();
} catch (e) {
if (!popupBlockingErrorShown) {
if (Popups[i - 1]) { //there is a previous popup and it's been focused on.
Popups[i - 1].alert(UIMessages[33]); //show alert on opened popup.
popupBlockingErrorShown = true;
}
else {
alert(UIMessages[33]);
popupBlockingErrorShown = true;
}
}
}
Thanks #wOxxOm !

jQuery $(window).on() does not work for popup window in IE11

I am trying to send data to a popup window which opens by clicking a button.
All my code is in php where I include javascript and html, I will just show html and js here.
Consider my popup window (say, popupwindow.html) code as just
<div id = 'getData'></div>
Now on the page(say, main.js) where I put the button clicking which the popup opens, I am computing some data and trying to send it to popupwindow.html.
Here is relevant main.js code that gets executed after clicking the button
var popup = window.open("popupwindow.html", "popup", extraParams); //ignore extraParams
$(popup).on('load', function() {
//console.log("inside onload function");
popup.document.getElementById("getData").innerHTML = data; //here data is some string
});
Now this code works perfectly on Chrome and Firefox, but in IE load() function does not get executed. I know this because the console.log line when uncommented, does not print on the console. Tried versions of jQuery from 1.2.3 to 2.2.1 where the code runs successfully on Chrome and Firefox(but not in IE)
Note: I cannot execute the required code on popup page(popupwindow.html) because of some constraints. I have to write it on main.js and send it to popup.
I saw all other questions which said onload() or similar function in IE gives problems but I still could not find the appropriate solution. Please let me know how I can fix this for IE.

closing browser within chrome page on test complete

So using TestComplete I'm essentially trying to open up a session on chrome, navigate to a web page, and then click a button on that page. after I'm finished I want to close that browser page. I'm having trouble closing the page though. Here is the code I have so far.
function ChromeTest
{
Browsers.Item(btChrome).Run(MyWebAdress);
var browser = Sys.Browser("chrome");
var page = Sys.Browser("chrome").Page(MyWebAdress);
var MyButton = page.ButtonLocation;
MyButton.click();
browser.BrowserWindow.Close(5000);
}
however, at the Close line I get an error that says "Unable to find the object BrowserWindow". Thanks in advance for any help you have.
Change BrowserWindow to BrowserWindow(0) (or whatever index you see in the Object Browser):
browser.BrowserWindow(0).Close(5000);
Or you can call Close() directly on the Chrome process:
browser.Close(5000);

Safari print issue with javascript window.print()

I am having an issue with print on Safari. My System is Windows 7, and this function works fine in all other browsers except Safari. Here is the situation:
window.onload = function(){
console.log('before print');
window.print();
}
It won't output the log in console panel, but the print page will appear first, after i choose cancel in print page, the log will be output.
Does any body came up with this issue? Any help will be appreciated.
Updated
Here is the situation i have:
We need to print a page whose content can be changed by user by checking and unchecking check box, and only the content part of this page should be printed, so we create a new page that only contains the content for printing. In this page, we need to hide the unnecessary content that is not selected by user, so we need to do some DOM operation before window.print() get called. The console.log() is just an example code for observing. I tried to add an <div id='test'>Test HTML</div> in test HTML and add
var test = document.getElementById('test');
test.style.background = 'yellow';
before window.print();, it shows the same result in my Safari browser, the 'Test HTML' will not turn to yellow until i click cancel button in print panel, so it's not just the console.log issue.
Updated
I am using Safari 5.1.7(7534.57.2) on Windows 7
For me, the setTimeout solution didn't work. I found this jQuery plugin https://github.com/jasonday/printThis that has plenty of workarounds for window.print() because it seems not to be fully supported by all browsers.
I took this line that worked for me Safari document.execCommand("print", false, null)
and this worked ok for me for now in safari and chrome
try {
document.execCommand('print', false, null);
}
catch(e) {
window.print();
}
This is odd behavior. I tested in Safari 6.1 on Mac.
But may I ask why you need to log something before the printing? Because it seems that all the functions are being executed before the printing panel pops up:
<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
<script>
window.onload = function() {
$('body').html('before print');
console.log('before print');
window.print();
};
</script>
When you look at the print preview, the page will have the text "before print" on it. For some reason, the console will log the text only when the print panel closes, but in my opinion that doesn't really matter for your visitors. You can manipulate DOM and change the page before the printing process as you like.
After several times trying, below code works, but i don't know the reason, can anybody explain? Or this is a Safari Bug?
window.onload = function() {
$('body').html('After change');
setTimeout(window.print, 1000);
};
Safari prints the page before it is loaded unlike other browsers. Hence window.onload() can be used in the code of the newly opened html page. But if the page opened is non html content, then it is not possible. The below solution is global across browsers and type of content open.
var printWindow = window.open(url, '_blank');
$(printWindow).load(function()
{
this.print();
});
Adding one more solution which worked for my case:
First make your popup window.
$( ".myButton" ).click(function() {
var url = 'www.google.com';
var printWindow = window.open( url, '_blank');
printWindow.focus();
});
Then, inside the HTML page which is loaded in the popup:
$(window).bind("load", function() {
setTimeout( function () {
try {
document.execCommand('print', false, null);
}
catch(e) {
window.print();
}
}, 500);
});

IE 8-9 can't focus to iframe

Hello I getting not visible IFrame in IE I try print it, but IE return error what focus() method not identified. Why I get it? I remember that some days ago it wored
var iFrame = document.frames["printIfram"]; //document.frames.printIframe;
iFrame.focus();
iFrame.print();
Put a print function inside the Iframe to make it print itself on load:
At the bottom of the page, just use window.print()
You missed the e on the end of printIframe.
If that's just a typo, try iFrame.contentWindow.focus().
i got the same problem in ie8 and i thought it's a bug of ie
i tryed the accepted answer ,it haven't work yet;
nomatter where is the window.print(),it always print the parent page
so ,i give up the iframe,just open a new tab to print
have you got it work?

Categories