This may be a specific issue with Chrome that I should take to their forums, but I wanted to see if there was an alternate solution or a fix. We are using backbone.js for our single page app and to print, we create a new window, write our html to it and then call print on the new window. If a user closes the tab without closing the print dialog, the hashchange and popstate events do not fire anymore in the Backbone.History object. You can't refresh the page either. We have to close the page and reopen in a new tab to restart.
This error does not occur on linux builds, just windows. If the user closes the print dialog first and then the tab, everything works normally.
The ideal solution would be for the hashchanges to keep working. If this isn't possible, is there another solution to do a print of a certain portion of HTML?
I've tried writing a script that calls window.print() in the new window but it does not fire or even throw an error. IFrames will not work because the css of the single page app will overwrite the printing portions html. Any solutions are welcome.
Here is a jsfiddle to show you the problem.
http://jsfiddle.net/5P4qv/3/
window.document.getElementById('run_print').onclick = function () {
window.onhashchange = function () {
console.log('hashchanged');
};
window.location.hash = 'test';
windowObject = window.open("", "_blank");
windowObject.document.open();
windowObject.document.close();
windowObject.focus();
windowObject.print();
};
You may need to allow popups for this to work. Click the print button and the popup will open to the print dialog. Close the window without closing the print dialog and the original window will act as if it is still loading. You will not be able to refresh either.
Again, this is only on Chrome in windows.
Related
I have a new window that I open with
window.open()
In the new window I run a javascript function with the onLoad option in the <body> tag of the New window's HTML.
Within the JS associated with new window, I call window.print(). This causes the Confirm Dialog Preference box to open in Firefox 42.0b8.
Is there a way to avoid this behavior(the prompting of the Confirm Dialog Preference) or is it inevitable if I am calling window.print() from a new window?
I ran into this same issue. The fix is to:
Hit Shift+F2 - this opens the developer command line at the bottom of the browser
in the developer command line, type in:
pref set dom.successive_dialog_time_limit 0
and hit enter.
This was a tough one to track down. It's not a programmatic solution, but hope it works out for you.
I have MyPage.aspx html page (generated using ASP.Net). When user tries to navigate away from this page, I need to close the window – user should not be able to go back or navigate to another page.
When I used window.close() inside window.onbeforeunload event, it asks for a confirmation to the user. “The webpage you are viewing is trying to close the window. Do you want to close the window?” On clicking “No” the user can escape the close attempt. Is there any way to forcefully close the window without giving an option to the user?
Reference:
How can I close a browser window without receiving the "Do you want to close this window" prompt?
Html javascript to open new window and close current window
"Unknown Exception" when cancelling page unload with "location.href"
Display confirmation popup with JavaScript upon clicking on a link
You can "trick" the browser like this:
window.onbeforeunload = function() {
window.open('', '_self', '');
window.close();
}
It seems to work in chrome/safari/ie/ff: http://jsbin.com/olijig/1
Firefox seems stubborn, but there might be another way to do the same in FF.
I should probably say that this technique is in no way standard and I don’t recommend it at all, and this code might break in many browsers besides firefox.
UPDATE
It actually works in Firefox too (latest version), but not older versions (I tried 3.6.1). You need to do some more testing to confirm the browser compatibility.
No, you can't. The user must be always capable of controlling whatever happens in his browser.
I'm not positive about this, but I believe if you have a window open another window, the parent window can close that child window. Would it be practical to have a landing page that opens your app in a separate window that could then close the window through javascript? Someone can probably elaborate more, as I haven't done this myself.
If I open my extension popup then I open another window or tab following the popup does not stay open if I return to it.
Is there a way to force it so the popup stays open?
As a user, you currently cannot force the the popup to stay open. That is a UI decision the UI team made. If you want to want to force a setup, you can have other way to show this by changing the popup icon, open a new tab when it requests, or new popup view for registration.
As a developer, inspect the popup, and it will stay open.
You cannot stop the Chrome pop-up from closing, unless you're in developer mode. You could consider this alternative, though:
Launching a normal pop-up instead:
In your popup.html file, load a Javascript file that runs this:
var popupWindow = window.open(
chrome.extension.getURL("normal_popup.html"),
"exampleName",
"width=400,height=400"
);
window.close(); // close the Chrome extension pop-up
This will open the file normal_popup.html in your extension in a normal pop-up window, which won't close when it loses focus. Because the name parameter is the same, the pop-up window will get reused if the user launches popup.html again.
In an answer to a FAQ here: https://developer.chrome.com/docs/extensions/mv3/faq/#faq-persist-popups
Popups automatically close when the user focuses on some portion of the browser outside of the popup. There is no way to keep the popup open after the user has clicked away.
As others have said, this is a deliberate limitation of popup UI.
Instead, you could inject some HTML into the page which loads the content you want in your popup into an element which hovers over the existing page. You will have to implement the close functionality yourself, but it will persist.
Have a look at e.g. how keyframes.app has done it: https://github.com/mitchas/Keyframes.app/blob/master/Keyframes.app%20(Extension)/src/inject/ui.js
If you enable panels at "chrome://flags/#enable-panels" you can use something like:
chrome.windows.create({
url:"popup.html",
type:"panel",
width:300,
height:200
});
to open a panel window instead which will stay on top all the time as long as you don't move it from the bottom of the screen.
Best way to workaround this is to :
Right-Click inside the popup
Click: Inspect
Or just press CTRL+Shift+I
A new window will open with the Developer Tools... just keep that window open and the popup will never close.
This answer to How do I prevent Chrome developer tools from closing when the current browser window closes? what very helpful in my case:
Not a perfect solution, but you can add breakpoints on the events Window.close and unload by turning on the checkboxes at:
Developer tools -> "Sources" tab -> Event Listener Breakpoints -> Window -> close
And
Event Listener Breakpoints -> Load -> unload
Try to mark both and see which one works best for you
I have a popup window and in that page I have the following code in the body.
<img src="...something"/>
The purpose is to have this popup window close when a user clicks on the image link, and to open a new page and be directed to http://www.example.com.
It works in IE and Chrome, but not in Firefox. The popup window closes but no new window is opened.
Any ideas?
Yes, I can repro this - interesting. setTimeout works around it:
onClick="javascript: setTimeout(window.close, 10);"
I can only guess that once the window closes (which happens before the hyperlink is followed) Firefox stops processing that page.
Edit: better make it 10ms delay - with 1ms Chrome doesn't close the window.
The question is actually solved for the opener but it didn't help my issue (not wished: the new windows under firefox keep the same size as the current popup).
So I find following solution:
function whenClicked()
{
window.close();
opener.location.href = "http://www.example.com";
}
or this if the ppage should open in a new tab:
function whenClicked()
{
window.close();
opener.open(http://www.example.com, '_blank');
}
When you add some functionality to an element's click event via javascript, that functionality is executed before the default click event (in this case, opening a new page), in order to allow for the possibility of intercepting and overriding the default event. The default behavior will only execute when and if the event returns a boolean value of true.
In this case, the additional functionality would be to close the window and my guess is that Firefox chooses to interpret this as "we're all done here", so the click event never returns true, and thus the new page never gets opened.
Evgeny's suggestion of using a short timeout would allow the click event to return true before the window is closed, thus allowing the new window to open.
I have some JavaScript that makes an AJAX call and, if the call fails, opens a new windows (tab in Firefox) and displays the response from the server in that window. This is very convenient for debugging, because the error is typically from Pylons, so it's a full HTML page.
The only problem is that the new tab becomes the active tab, which would totally confuse a regular user. Is there any way to open the tab/window, but not make it active, ie. keep the current active window?
My code currently looks like this:
errorWindow = window.open("", "TCerrorWindow")
if (errorWindow)
errorWindow.document.write(xhr.responseText);
You can call errorWindow.blur(); window.focus(); after, forcing the browser to return focus to the previous window.
The effect you're trying to achieve is commonly called a pop-under window.
AFAIK this is not possible, as a security measure against pop-under windows. For debugging purposes you could
use Firebug (with a handy console, where you can output your own log messages from the code)
create a debug layer (div) on your page, where you output error messages in case an error happens