Check If Window Is Already Open Window.Open

Check if window is already open window.open

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 <a href="def.html" target="com_MyDomain_myWindowForThisPurpose">clicky!</a> will re-use it. Normal caveats about browsers resisting window-opening in various scenarios (popup-blocking) apply.

Check if window is already open from a non-parent window (chrome extension)

no need to track windows and store them.

if you know your extension ID, the simplest way is to test all tabs url's and see if it's already opened

chrome.tabs.query({}, function(tabs) {
var doFlag = true;
for (var i=tabs.length-1; i>=0; i--) {
if (tabs[i].url === "chrome-extension://EXTENSION_ID/feedback-panel.html") {
//your popup is alive
doFlag = false;
chrome.tabs.update(tabs[i].id, {active: true}); //focus it
break;
}
}
if (doFlag) { //it didn't found anything, so create it
window.open('feedback-panel.html', 'Feedback', 'width=935, height=675');
}
});

and here is already answered how to get extension ID,

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

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

To open the already opened tab using window.open() function without reloading the already opened tab

You can store the window object into a variable and call the focus() method multiple times like this:

let newWindow = window.open(url,'dialers')
newWindow.focus()
doThing()
newWindow.focus()
doAnotherThing()
newWindow.focus()

JavaScript window.open only if the window does not already exist

I'd do it like this - basically store all the referenced opened windows on the function itself. When the function fires, check if the window doesn't exist or has been close - of so, launch the popup. Otherwise, focus on the existing popup window for that request.

function launchApplication(l_url, l_windowName)
{
if ( typeof launchApplication.winRefs == 'undefined' )
{
launchApplication.winRefs = {};
}
if ( typeof launchApplication.winRefs[l_windowName] == 'undefined' || launchApplication.winRefs[l_windowName].closed )
{
var l_width = screen.availWidth;
var l_height = screen.availHeight;

var l_params = 'status=1' +
',resizable=1' +
',scrollbars=1' +
',width=' + l_width +
',height=' + l_height +
',left=0' +
',top=0';

launchApplication.winRefs[l_windowName] = window.open(l_url, l_windowName, l_params);
launchApplication.winRefs[l_windowName].moveTo(0,0);
launchApplication.winRefs[l_windowName].resizeTo(l_width, l_height);
} else {
launchApplication.winRefs[l_windowName].focus()
}
}

Javascript Check if window was opened by script

window.opener will be null when the page is not opened by another window, and therefore can not be closed.

This code should should do the trick:

function goAway() {
if(window.opener === null) {
window.location.href = '<YOUR_DEFAULT_URL>';
}
else {
window.close();
}
}

You can find more details here



Related Topics



Leave a reply



Submit