This is a follow up to my last question Open a window if the window does not already exist Essentially, I am now keeping a list of all the window references that have been opened by a page, and only allowing them to be opened if they are not already open. Then a potential problem struck me - it is of course possible for a user to shut down the original window, and open it again, thus losing the list of window references.
Is it possible to loop through the windows open in a browser, checking for a particular URL?
Edit: After a lot of helpful comments here (and on the other question), here is the final code for the application launcher. Essentially, it tries to get the location of the open window with the appropriate name. If that causes an exception (because of a privacy issue), then the application is judged to have been loaded. If it is "about:blank", then it is a new window. This works on Firefox, IE7 and Google Chrome. It feels dirty...
var g_urlarray = [];
Array.prototype.has = function(value) {
var i;
for (var i in this) {
if (i === value) {
return true;
}
}
return false;
};
function launchApplication(l_url, l_windowName)
{
var l_width = screen.availWidth;
var l_height = screen.availHeight;
var winRef;
var l_params = 'status=1' +
',resizable=1' +
',scrollbars=1' +
',width=' + l_width +
',height=' + l_height +
',left=0' +
',top=0';
if (g_urlarray.has(l_url)) {
winRef = g_urlarray[l_url];
}
if (winRef == null || winRef.closed) {
winRef = window.open('', l_windowName, l_params);
var l_openNew = 0;
try {
if (winRef.location == 'about:blank') {
l_openNew = 1;
}
}
catch (e) {
l_openNew = 0;
}
if (l_openNew === 1)
{
winRef.location = l_url;
winRef.moveTo(0,0);
winRef.resizeTo(l_width, l_height);
}
g_urlarray[l_url] = winRef;
}
}
Open in specific window. Chrome will by default open any link in the same window as it was clicked. If you have multiple chrome windows open, this extension will add a right-click menu allowing you to choose which window to open the link in.
The JavaScript window. open() method allows you to open URL in the browser tab or window. You can use _self value in the second parameter of the window. open() method to open URL in the same tab and in the same window with JavaScript.
To open a new window, use a keyboard shortcut: Windows & Linux: Ctrl + n. Mac: ⌘ + n.
No, this would be a security/privacy issue.
Since others have brought up the ownership/cookie state storage: this only works if you are also the same document which opened the window, i.e. in the scenario where the user shuts the window down and reopens then these references are indeed lost (and even if you stored them, you wouldn't have permission to close them any more)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With