I need a popup window to appear to the user but without a parent window. However, I believe this to be impossible.
I think that I may have to make the popup close the parent after it appears, but how?
iI tried this in the popup:
<BODY align="center" valign="center" onload="javascript:window.opener='x';window.close();">
But it closed the popup window itself. How can I make the popup close its parent?
Try window.opener.close() instead.
Unless the "parent window" was opened with JavaScript itself, then that is not possible. JavaScript is only allowed to close windows it opened itself.
Related
I haven't found a single answer able to tell me what's the right way to open a popup.
Times have changed, and popups have been mostly replaced with fancybox-like boxes. However, there are still times when popups are needed.
For those cases, I don't want my popup to be blocked by the browsers.
What's the right way to open a popup without it being blocked? Even if it opens a new tab in the browser. I just want my popup to be open, and have control of it from the parent or vice versa.
Popup blockers will block any popup, unless it is opened because of an user action.
If the user clicks on a link, and a popup is opened in the click listener of that link, the popup blocker knows the user want to open something and will not (or should not) block the popup.
What you cannot do:
open a popup when the page is opened or closed
open a popup after a certain interval
open a popup after something asynchronous happens
What you can do:
open a popup in the on click listener
using target="_blank" in a anchor tag
You can access both windows with JavaScript variables:
if you use window.open, the parent can have a reference to the popup by assigning the result of window.open to a variable. Check out this article at W3Schools.
If the popup needs to have access to the window who has opened it, you can use window.opener. Check out this question.
try this, it works for me
$('#myButton').click(function () {
var redirectWindow = window.open('http://google.com', '_blank');
redirectWindow.location;
});
Js fiddle for this is here https://jsfiddle.net/safeeronline/70kdacL4/2/
if you want to open new tab after ajax call see this fiddle http://jsfiddle.net/safeeronline/70kdacL4/1/
I have an application, in that I want to close the entire browser on clicking on the close button from our application. I tried with the window.close() and self.close() functions but they will be closing the browser if the window is opened from window.open().
Please suggest to me how I can close the browser on clicking on the close button?
You can't. window.close() used to do that in a far past, but browsers don't allow that anymore, going from the concept of that a website should be able to stay open as long as it has to. Eg. a website doesn't end. If you log out, you don't close the window, you simply go back to the home page where you let the user log in again. Same with smartphone apps btw :-)
Most modern browsers will only let you close child windows of a parent. You cannot close the parent window through script.
I've got a page which open a new window fullscreen but it still has the other page (the one which re-directs) behind so when you close the new window you are taken back to a blank page.
Is there a way to open a new window and then close the tab which is now inactive?
Currently I have some javascript like this
window.open("http://website.co.uk");
Thanks
You can technically use window.close() on the opening tab, but browsers usually will not allow JavaScript to close windows that it has not opened. Can you redirect to some useful page that the user can use once they have closed the window?
If you can add some JavaScript code to the page being opened, I think you can use window.opener.close() to close the original window. I tested it in IE8, the browser will ask for confirm before close the original window.
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
window.close();
The above will pop up a confirm dialog each time, and not working at all in Firefox.
Use this way
window.open('','_self');
window.close();
I think that's not without a reason. People don't like windows being closed without notice.
Closing a browser window is not as straight-forward as it used to be years ago.
Typically, a newly opened window can be closed if:
1. the 'close' is called within the DOM of the window itself
2. the closer is the opener
However, with almost all browsers having tabs, if the opened window is the only remaining tab in the main window, it might not close without prompt for above case 1. Even if it closes, it might just close the tab and leave the main window opened.
You can't - it's a security feature. You'll need to look into showing some form of modal dialog if you wish to be able to close it. Have a look at something like This JQuery Example which features auto-close
Windows not opened by JavaScript cannot be automatically closed with JavaScript (and I can't think of any good reason for a website to close the window that the visitor arrived at the site with, discarding their Back history in the process)
it works in chrome,firefox need to turn the "allowjavascriptclosewindow" option on.
ie need alter your code as:
window.open("","_self");
window.close();
The below javascript works fine to close the tab with user confirmation.
<script>
function closeWindow()
{
if (confirm('Are you sure you want to close the Window? All the unsaved data will be lost')) {
top.window.open('','_self','');
top.window.close();
}
}
</script>