js popup open and check it's status on another page - javascript

open a popup window using window.open() on on html page
pop=window.open('pops.html','mypop');
when i go to new page how can i detect whether this popup still opened or closed using java script. Thank you

You could use window.opener.methodYouWantToCall() to call methods in the window that opened the new window to talk to the opening window and tell it things like "I'm still open". you can read more here. You will have to make sure that the method exists in the window though.
here is some pseudo code for how to handle it:
my_window = window.open(...);
my_window.opener.document.onUnload = function(){
my_window.opener.document.onload = function(){
my_window.opener.theWindow(a variable) = my_window;
}
};
(in new page):
function check(){
if(theWindow != null){
if(!theWindow.closed){
// handle
}
} else {
setTimeout("check()", 1000);
}
}

Related

Detecting when popup window is closed in Firefox

I open a new window like this:
var newWindow = window.open('myPage.aspx', null, 'height=650,width=900,status=yes,toolbar=no,scrollbars=yes,menubar=no,location=no,top=0, left=0');
And I wait for it to close:
var windowTimer = window.setInterval(function () {
if (win.closed !== false) {
//If window is closed ...
window.clearInterval(windowTimer);
}
}, 100);
This does work in Chrome and IE9 and Edge but not in Firefox, why?
Firefox does get inside the function but it never gets on win.closed if, even if there is an else it neither goes into it... is there any alternative to this?
Solution that worked for me:
On the popup window:
//Fires an event on the window that opened it
window.onbeforeunload = function () {
window.opener.myEvent();
};
On the main window:
window.myEvent= function () {
//This fires only when the popup gets closed on beforeunload
}
Note: the event to fire in the main window must be declared as public so it can be accessible, like so window.myEvent= function ()
Another reference: cross-window javascript events
Basically, I think the simplest way to do this is the following:
function hasClosed(){
document.getElementById('open').textContent = 'window has been closed';
}
window.addEventListener('beforeunload', function(){
if(window.opener) window.opener.hasClosed();
});
document.getElementById('open').addEventListener('click', function(e){
e.preventDefault();
window.open('test.html');
});
open window
Please keep in mind that SO snippets are not allowed to open windows, so the code is not functional inside the snippet. Simply copy the code and save it as a file called test.html.
You do not need to keep checking, the child will simply call the function on the parent. This has the advantage that you are not using resources to keep checking for it. Also, be aware that when navigating in this window, onbeforeunload gets called if a new page is loaded, calling the parent function, but maybe you could just do the check you were already doing in the hasClosed function.
You might want to give an uuid to your window and pass it into it so it can inform you of it's identity on closing, but that's all refinement.

Javascript | Check if a windows with specific URL already is opened

This functions show a /ticket.html popup window.
I need to check before if the window is already opened. If it is, cancel the new openning.
How could this be done?
function popitup() {
newwindow=window.open("ticket.html","_blank","toolbar=yes,scrollbars=yes, resizable=yes, top=500, left=500, width=730, height=700");
newwindow.moveTo(350,150);
if (window.focus)
{
newwindow.focus()
}
}
Regards,
You may save a reference to the window, when opening it. The window.open method returns a windowObjectReference.
With this reference, you can check if the window is closed (closed property, a boolean) or simply if the window is null (window property, which will be a Window object or null, if closed).
Simple example:
// Open the pop-up and save the reference.
var windowObjRef = window.open('ticket.html');
// Verification, alternative 1. You may encapsulate this in a method.
if (windowObjRef.closed) {
// The window is closed.
else {
// The window is still open.
}
// Verification, alternative 2. You may encapsulate this in a method.
if (windowObjRef.window) {
// The window is still open.
else {
// The window is closed.
}
Reference: https://developer.mozilla.org/en-US/docs/Web/API/Window
I'm new to javascript. I found a website giving the following code. But it gives information on only whether the window is created earlier or not.
function myOpenWindow(winURL, winName, winFeatures, winObj)
{
var theWin; // this will hold our opened window
// first check to see if the window already exists
if (winObj != null)
{
// the window has already been created, but did the user close it?
// if so, then reopen it. Otherwise make it the active window.
if (!winObj.closed) {
winObj.focus();
return winObj;
}
// otherwise fall through to the code below to re-open the window
}
// if we get here, then the window hasn't been created yet, or it
// was closed by the user.
theWin = window.open(winURL, winName, winFeatures);
return theWin;
}
Also, accessing the contents of other tabs may also bring attack on privacy of the user.

How to get count of browser open window or tab? [duplicate]

I have a html page. In the body of the page I am calling onload event which calls javascript function to open a pop up window. here is the code:
var newWindow = null;
function launchApplication()
{
if ((newWindow == null) || (newWindow.closed))
{
newWindow = window.open('abc.html','','height=960px,width=940px');
}
}
when I move to another page, and come back to that page again, popup reopens, although it is already opened. Please guide me to proper direction so that if pop up is already open then it should not open again. I tried document.referred but it requires the site online, currently I am working offline.
newWindow = window.open('abc.html','com_MyDomain_myWindowForThisPurpose','height=960px,width=940px');
Give the window a name. Basing the name on your domain like this, prevents the chances of you picking a name someone else happened to choose.
Never make up a name that begins with _, those are reserved for special names the browser treats differently (same as with the "target" attribute of anchor elements).
Note that if the window of that name was opened with different options (e.g. different height), then it'll keep those options. The options here will only take effect if there is no window of that name, so you do create a new one.
Edit:
Note that the "name" is of the window, not of the content. It doesn't affect the title (newWindow.document.title will affect that, as of course will code in abc.html). It does affect other attempts to do stuff across windows. Hence another window.open with the same name will reuse this window. Also a link like clicky! will re-use it. Normal caveats about browsers resisting window-opening in various scenarios (popup-blocking) apply.
To open a window and keep a reference to it between page refresh.
var winref = window.open('', 'MyWindowName', '');
if(winref.location.href === 'about:blank'){
winref.location.href = 'http://example.com';
}
or in function format
function openOnce(url, target){
// open a blank "target" window
// or get the reference to the existing "target" window
var winref = window.open('', target, '');
// if the "target" window was just opened, change its url
if(winref.location.href === 'about:blank'){
winref.location.href = url;
}
return winref;
}
openOnce('http://example.com', 'MyWindowName');
You can check if the window is open or closed by re-assigning a reference to it when it closes. Example:
var newWindow;
var openWindow = function(){
newWindow = newWindow || window.open('newpage.html');
newWindow.focus();
newWindow.onbeforeunload = function(){
newWindow = null;
};
};
Use the "closed" property: if a window has been closed its closed property will be true.
https://developer.mozilla.org/en-US/docs/Web/API/Window/closed
When you move on another page (on the same domain), you can re-set the window.open variable with popup page like this :
https://jsfiddle.net/u5w9v4gf/
Step to try :
Click on Run (on jsfiddle editor).
Click on Try me (on preview).
Click on Run to move on another page, the variable will be re-set.
Code :
window.currentChild = false;
$("#tryme").click(function() {
if (currentChild) currentChild.close();
const child = window.open("about:blank", "lmao", 'width=250,height=300');
currentChild = child;
//Scrope script in child windows
child.frames.eval(`
setInterval(function () {
if (!window.opener.currentChild)
window.opener.currentChild = window;
}, 500);
`);
});
setInterval(function() {
console.log(currentChild)
if (!currentChild || (currentChild && currentChild.closed))
$("p").text("No popup/child. :(")
else
$("p").text("Child detected !")
}, 500);

How to update global variable when window closes?

First time here. I'm trying to code something with these features with Javascript.
First, if a link is clicked, and there is no existing popup window, a popup window will be created, navigating to that link.
However, if second link is clicked, and there is an existing popup window, it'll run some AJAX function. We will not navigate to that link.
However, if another link is clicked, and the popup window has been closed, it'll open the window again (and navigate to that link).
The only way I can think of that allows me to solve this is using a global variable, however it's not working out. Can someone help please? Thanks!
Here's my jsfiddle
The HTML
1
<br/>
4
The Javascript
var displayWindow = null;
var test = 'test';
function openwindowPreview(id, winObject) {
// check if the window already exists
if (winObject != null) {
// the window has already been created, but did the user close it?
// if so, then reopen it. Otherwise make it the active window.
if (!winObject.closed) {
winObject.focus();
return winObject;
}
}
if (test != 'test') {
if (winObject.closed) {
test = 'test';
} else {
alert('ajax');
}
}
// if we get here, then the window hasn't been created yet, or it
// was closed by the user.
if (test == 'test') {
var urlDisplayID= "file.php?ID=" + id;
window.open(urlDisplayID, 'width=' + screen.width, 'height=' + screen.height);
test = 'tested';
}
}
Basically, only allow one instance of the window at once, and only display the first instance, while other instances (different URLs — because of the parameter) are sent to the server via AJAX.
in your case I think this maybe as your need.
var openWindows = {};
function openwindowPreview(id) {
// if we get here, then the window hasn't been created yet, or it
// was closed by the user.
var urlDisplayID= "file.php?ID=" + id;
//set id as the window name, so if the window already opened,
//the open method will find the opened window with the window name
var opened = window.open(urlDisplayID, id,'width=' + screen.width, 'height=' + screen.height);
if(opened === openWindows[id]){
alert("ajax");
}else{
openWindows[id] = opened();
}
}

How to redirect main window to URL from popup?

I have a pop-up window with a form in it. On submit of the form, I wish to redirect to a particular page, but on the parent window (not on the popup).
How can I achieve this using Javascript?
After Application of Josh Idea
I am calling a javascript function to submit a form, in this javascript, below is the mentioned code
So Can this be executed as i tried with this and its not working as per my need
function instant_popup_post()
{
var cid = document.getElementById('product').value;
var session_id = document.getElementById('sessid').value;
if(cid==30)
{
alert(document.getElementById('instantpop').onsubmit="opener.location.href = 'http://192.168.1.5/cppl11/bannerbuzznew/full_color_banner.php?&id=+cid+'&info_id=5&osCsid='+session_id;");
document.instantpop.submit();
}
else if(cid==31)
{
document.getElementById('instantpop').onsubmit="opener.location.href ='perforated_window_signs.php?&id='+cid+'&info_id=6&osCsid='+session_id;";
document.instantpop.submit();
}
else if(cid==32)
{
document.getElementById('instantpop').onsubmit="opener.location.href ='preprinted_stock_banner.php?&id='+cid+'&info_id=7&osCsid='+session_id;";
document.instantpop.submit();
}
}
plss help
From within the popup, you can use the opener property to reference the parent window...
opener.location.href = 'http://www.google.com';
You can also invoke functions on the parent window...
opener.functionName();
Of course, the good old same origin policy restrictions apply here
I would say to use showModalDialog, so you will be freezing the parent window, and after it is done, you can send a variable to parent and do the redirect:
MainWindow:
function ShowModalForm()
{
var urlToRedirect = window.showModalDialog('url');
if (urlToRedirect)
window.location = urlToRedirect;
}
Popup Window:
function buttonAcceptClicked()
{
//Do stuff you need
window.returnValue = "new url";
window.close()
}
Here is a lot of information about this.

Categories