window.close current window when another window.open is requested - javascript

Thank you for taking the time to look at my question.
I have two different window.open requests depending on if a user clicks on either a mobile button or a webcam button.
a mobcamWindow will open with a certain (small) size when the mobile button is clicked
a webcamWindow will open with a different (large) size when the webcam button is clicked
These windows will open fine and in the correct size, however if the webcam button is clicked while the (smaller) mobcamWindow is open, I want the current mobcamWindow to close and the new (larger) webcamWindow to open (in its correct size) and vice versa.
Currently, the window does not close, but the URL will change to the correct mobcamWindow & webcamWindow respectively, however the window will remain the same size.
I have tried to do window.resize which did not work, and window.close does not work either.
My code is as follows. (I have two functions to handle each mobcamWindow & webcamWindow, with an IF statement in each to check if mobcamWindow or webcamWindow is open, and to close it if it is.
var webcamWindow = null; // global variable
var mobcamWindow = null; // global variable
function openStreamPopupWebcam(elem) {
if(webcamWindow == null || webcamWindow.closed)
/* if the pointer to the window object in memory does not exist
or if such pointer exists but the window was closed */
{
if(mobcamWindow){
mobcamWindow.close();
}
/*if mobile window is open, close mobile window*/
webcamWindow = window.open(elem.href, "window", "width=470,height=320,resizable,status").focus();
/* then create it. The new window will be created and
will be brought on top of any other window. */
}
}
function openStreamPopupMobile(elem) {
if(mobcamWindow == null || mobcamWindow.closed)
/* if the pointer to the window object in memory does not exist
or if such pointer exists but the window was closed */
{
if(webcamWindow){
webcamWindow.close();
}
/*if mobile window is open, close mobile window*/
mobcamWindow = window.open(elem.href, "window", "width=445,height=666,resizable,status").focus();
/* then create it. The new window will be created and
will be brought on top of any other window. */
}
}
If you need any more information or are finding it hard to understand me please ask and I will try and explain as best I can.
Thank you.

You have to reinitialize the values to 'null' when those conditions are satisfied
var webcamWindow = null; // global variable
var mobcamWindow = null; // global variable
function openStreamPopupWebcam(elem){
if(webcamWindow == null || webcamWindow.closed){
if(mobcamWindow){
mobcamWindow.close();
mobcamWindow = null;
}
webcamWindow = window.open(elem.href, "window", "width=470,height=320,resizable,status").focus();
}
}
function openStreamPopupMobile(elem){
if(mobcamWindow == null || mobcamWindow.closed){
if(webcamWindow){
webcamWindow.close();
webcamWindow = null;
}
mobcamWindow = window.open(elem.href, "window", "width=445,height=666,resizable,status").focus();
}
}

Answered by Teemu
Give different names for your windows, now both are named as window. – Teemu
Thank you.
Also thank you Adarsh Mohan for pointing out that the values should be reinitialized to 'null' on close.

Related

Prevent child window from closing when parent window is closed in Electron

I'm making an Electron app. This app has a way to open a new window (popup) to be dragged on a different monitor. This window has a way to open another one, and so on. I need the user to be able to close a window he does not want, but keep others open.
First, I tried window.open but the child window gets closed when the parent is closed. I thought it must be because the var gets garbage collected.
Second, I tried binding to the new-window event in the main process.
This is what I did:
let windows = [];
function createNewWindow(){
let win = new BrowserWindow(
{
width: 1600,
height: 900,
webPreferences: {
nodeIntegration: true
}
}
);
win.webContents.on('new-window', (event, url) => {
event.preventDefault()
let win = createNewWindow();
win.loadURL(url);
event.newGuest = win;
})
windows.push(win);
return win;
}
But the child gets closed when the parent is closed. I checked the windows var, but it is correctly retained in the main process, so this should not be a GC problem.
How can I open a chain of windows (with or without window.open) without them being closed when the main parent is closed?
EDIT
As I did not found a way to keep windows open, I decided to hide the windows instead of closing them. This is what I did:
win.on("close", (event) => {
if (win.hideInsteadOfClose == true) {
event.preventDefault();
win.hide();
}
});
Where hideInsteadOfClose is a property I give to new windows created. This is not the proper way of doing it, but it gets the work done. Please feel free to answer with the correct way.
As I did not found a way to keep windows open, I decided to hide the windows instead of closing them. This is what I did:
win.on("close", (event) => {
if (win.hideInsteadOfClose == true) {
event.preventDefault();
win.hide();
}
});
Where hideInsteadOfClose is a property I give to new windows created. This is not the proper way of doing it, but it gets the work done.
Please feel free to answer with the correct way.

Open a new tab if it is not already open

$('#cptagswrap').click(function() {
window.open('tags.php');
});
This opens a new browser tab and load tags.php.
I need firstly to check it tags.php is somewhere already open.
If yes, then go to the tab without opening a new one.
If no, then open it.
Something like:
if (tags.php).is(':open') {goto tags.php;}
else {window.open('tags.php');}
Any help?
Copied from documentation that #Amy says:
var windowObjectReference = null; // global variable
function openFFPromotionPopup() {
if(windowObjectReference == null || windowObjectReference.closed)
/* if the pointer to the window object in memory does not exist
or if such pointer exists but the window was closed */
{
windowObjectReference = window.open("http://www.spreadfirefox.com/",
"PromoteFirefoxWindowName", "resizable,scrollbars,status");
/* then create it. The new window will be created and
will be brought on top of any other window. */
}
else
{
windowObjectReference.focus();
/* else the window reference must exist and the window
is not closed; therefore, we can bring it back on top of any other
window with the focus() method. There would be no need to re-create
the window or to reload the referenced resource. */
};
}

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();
}
}

Categories