window.print() from Chrome subwindow - close, then breaks - javascript

When in our webapp a Chrome subwindow is launched, either by window.open() or by user clicking a link with target="_blank", then in that subwindow, body onload="window.print()" to auto-launch the print dialog and print preview, then user CLOSES the print/subwindow instead of clicking cancel, the parent window gets completely hosed. Specifically:
No javascript events will fire
No links are clickable
Hitting F5 shows the little spinner in the tab but the page never reloads.
The parent window is truly dead -- all you can do is close it.
If you click cancel on the subwindow (where the print-preview is launched via window.print()) everything is fine. But if user closes the window, all the craziness happens.
This is a known bug in Chrome:
bug 1
bug 2
bug 3
Does anyone know of a workaround for this?

Instead of using window.print(), just bring your content to new window and then call print functionality as given below, to print the content.
following is a function call where we passed element's inner html through it's id to new window.
PrintContent(document.getElementById('div-content-id').innerHTML);
function PrintContent(printableContent) {
var printWindow = window.open("", "Print_Content", 'scrollbars=1,width=900,height=900top=' + (screen.height - 700) / 2 + ',left=' + (screen.width - 700) / 2);
printWindow.document.write(printableContent);
printWindow.document.close();
printWindow.focus();
printWindow.print();
printWindow.close();
return false;
}
I was also facing same problem, it works for me.

Related

How to return focus on parent window from a pop window while loading first time

I am working on a website. Where while loading website first time opening index page with one pop window. First my problem is I want to minimize this pop window automatically while loading first time and focus return to website without any click in website.
Here is my code in index page I write this function:
myFunction();
function myFunction() {
//var myWindow = window.open("music.php", "", "width=320,height=60");
var myWindow = window.open("music.php", "", "directories=0,titlebar=0,toolbar=0,location=0,status=0,menubar=0,scrollbars=no,resizable=no,left=-100000000, top=100000, width=320, height=1, display=none","visibility=hidden");
//myWindow.moveTo(0, 0);
}
And pop window having page extension music.php which should be minimize automatically and focus return to index page without any click on website.
I have already try with set the focus of a popup window to website everytime , but it was also not working.
Well, The window object has an opener property as well.
When we open the browser, that property is null, and when we open a popup , it points to the window the popup has been opened through.
I think something like this should suffice.
myFunction();
function myFunction() {
var myWindow = window.open("music.php", "", "directories=0,titlebar=0,toolbar=0,location=0,status=0,menubar=0,scrollbars=no,resizable=no,left=-100000000, top=100000, width=320, height=1, display=none","visibility=hidden");
myWindow.opener.focus() // can also do window.focus()
}
The execution of JS should not stop, but if it does, you have to add the focus code to your child page/popup page.(Depending on varying browsers)
like
popupHTMLJS.js
focusParent();
function focusParent(){
parent = window.opener;
parent.focus()
}

How to open popup window with window.open() having the confirmation message as Yex/No?

I am opening a pop-up window having the Message asking for confirmation that "Do you want to delete a record" with Yes/No buttons. But, its not working correctly. Below is the code:
var modal = window.open (sUrl,"", "dialogHeight:" + iHeight + "px; dialogWidth:" + iWidth + "px;status=no;scrollbars=yes;resizable=no;titlebar=no;", null);
modal.dialogArguments = messageObject;
As the program goes to this line it opens the pop-up window but does not load it and proceeds further finishing the whole JavaScript code. I want the program to stop till it gets confirmation from the child window Yes/No.
Note: I am getting the value of Yes/No using a hidden variable in the parent window. Here is the code written in child window and which gets me the confirmation value:
window.opener.setMsgValue(returnValue);
This is the function written in the parent window:
function setMsgValue(msgSel) {
document.getElementById('msgSel').value = msgSel;
}
I know I can do this using the window.showModelessDialog. But, this works only in IE8 and I am doing this code for chrome 39.
You could use something like
if (confirm("Are you sure?")) {
// Delete it!
} else {
// Do nothing!
}
It's shorter, but every browser styles it different.

window.close(); not working when page changed or refreshed

I have this little function to open/close a popup player:
function popuponclick(popup)
{
my_window = window.open("folder/player-itself.htm", popup, "width=350,height=150");
}
function closepopup()
{
my_window.close();
}
I call the functions from HTML anchors that are on each page of the site (idea is to have the player stopped/started whenever you want)...now...
it works well until i change the page, or refresh the existing one - and from then the window can't be closed anymore. Any idea where i'm wrong? Tested in FF and IE8, same behavior.
Thanks for your help.
When you reload the original window (or tab), everything about the old one is gone, blasted into the digital void, never to be seen or heard from again. The bits literally disintegrate into nothingness.
Thus, the "my_window" reference you so lovingly saved when the second window was opened is gone for good, and the "my_window" variable in the newly-loaded window contains nothing. It's name is but a mockery of the variable in the now-dead page.
The only way to deal with this situation is for the popup window to periodically check back via "window.opener" to see if its parent page has been rudely replaced by some interloper. If that happens (and the new page is from the same domain), then the popup page can restore the reference to itself in the new page's "my_window" variable.
edit — OK here's a sample. You'd put something like this in the popup page, not the launching pages:
<script>
var checkParent = setInterval(function() {
try {
if (window.opener && ('my_window' in window.opener))
window.opener.my_window = window;
}
catch (_) {
// clear the timer, since we probably won't be able to fix it now
clearInterval(checkParent);
}
}, 100);
</script>
That's probably pretty close.

I need to open a new window in the background with JavaScript, and make sure the original is still focused

I have a window I'm opening with a Javascript function:
function newwindow()
{
window.open('link.html','','width=,height=,resizable=no');
}
I need it that once the new window opens that the focus returns to the original window.
How can I do that?
And where do I put the code - in the new window, or the old one?
Thanks!
This is known as a 'pop-under' (and is generally frowned upon... but I digress).. It should give you plenty to google about
You probably want to do something like:
var popup = window.open(...);
popup.blur();
window.focus();
Which should set the focus back to the original window (untested - pinched from google). Some browsers might block this technique.
After calling window.open, you may try to use
window.resizeTo(0,0);
window.moveTo(0,window.screen.availHeight+10);
this way can not really open window in background, but works in similar way. Chrome works fine, did not try other browser.
If Albert's solution doesn't work for you and you actually want the window visible, but to be opened behind the current window, you can try opening a new tab in the opener window and closing it right away, this will bring the focus back to the opener window.
window.open('link.html','','width=,height=,resizable=no');
window.open().close();
However, I believe whether the second window opens in a tab or a new window depends on your browser settings.
Please don't use "pop-unders" for evil.
You can use either
"blur" or
"focus" to do that required action.
"blur"
function newwindow()
{
var myChild= window.open('link.html','','width=,height=,resizable=no');
myChild.blur();
}
"focus"
function newwindow()
{
window.open('link.html','','width=,height=,resizable=no');
window.focus();
}
Put the code in your parentWindow (i.e. the window in which you are now)
Both will work.
tl;dr - in 2022 - ctrl/cmd clicking on a button and window.open(url, "_blank") in a javascript button handler's for loop will open multiple tabs in the background in Chrome.
I'm looking for this as of 2022 and none of the answers here worked (here and everywhere else I looked). My use case is clicking a button in a (progressive) web app which opens deep links to items in a list in background tabs (i.e. not "for evil").
It never occurred to me that ctrl/cmd + clicking on the button would open tabs in the background, but it does just as if the user clicked on an anchor tag itself directly - but only in Chrome. Combined with Chrome's relatively recent tab grouping feature, this can be very useful inside PWAs.
const isMozilla =
window?.navigator?.userAgent?.toString().toLowerCase().includes('firefox') ?? false;
for (let index = 0; index < urls.length; index++) {
const url = isMozilla ? urls.reverse()[index] : urls[index];
window.open(url, "_blank");
}
Note: I reverse() the array on Mozilla to get the order of newly created tabs as the user would expect them.
You can just use '_self'. It will be stay to the same page an
window.open(url, '_self');

window opener close issue for javascript

I have a problem with browsers window managament with javascript.
I have two page in my proof of concept application. First page contains login information (username, password, login button etc.) and second page is a managament screen. I need that when the user pressed to the login button on the login screen it open to main screen and main screen must be open new window without full screen. I mean close, minimize, maximize buttons and bottom bar of the windows os must be stayed on the screen.
During opening the new window on the login screen, it must be close itself automatically. I have found many example script but every script giving same results to me.
For example; following script solving my problem but same problems continue for me,
firefox does't close opener window it self,
ie 6.0 closing opener window - it's working
ie 7.0 - 8.0 before the close it self it asking "The webpage you are viewing is trying to close the window".
window.open("Content/StartPage.aspx", windowName, "menubar=0, location=0, resizable=1, status=1, width=" + screen.width + ",height=" + screen.height);
if (window.name != windowName) {
var me = window.self;
me.opener = window.self;
me.close();
}
How can i open new window and close the opener with above requirements without ask browsers question ?
Thank you.
You cannot do it according the security assurance of browser, there are some action which doesn't allow to be managed directly via javascript without user interference.
Try something like this in your new window, on the body onload:
function closeParent()
{
try
{
var op = window.opener;
op.opener = self;
op.close();
}
catch(er) {}
}
Still, this solution isn't perfect, I only got it to work in Internet Explorer, and even then, I got a warning popup for closing the parent window. This might be something that can't feasibly be solved.

Categories