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.
Related
$('#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. */
};
}
I am making a Chrome extension. Clicking a button in popup.html opens a new window and loads feedback-panel.html.
This works but, on click, I'd like to check if the window is already open and if so focus to it instead of creating a new one.
JavaScript window.open only if the window does not already exist looked promissing but it relies on the open windows being stored as variables on the parent page when the window is opened and checking those before opening a new one. This wont work for me since the parent window (popup.html) will often be closed and reopened itself and I'd lose the variables.
I tried to implement the same idea but store the window variables in with chrome.storage since it lets you store objects. Well, it does let you store objects but it serializes them first so the window variable loses all of it's functions and I end up with
result.feedbackPanel.focus() is not a function
Here is my attempt:
function openFeedbackPanel(){
chrome.storage.local.get('feedbackPanel', function (result) {
console.log( result.feedbackPanel); // logs window object sans all functions
if(result.feedbackPanel && ! result.feedbackPanel.closed){
try{
result.feedbackPanel.focus();
}
catch(error){
chrome.storage.local.remove('feedbackPanel', function () {
});
createFeedbackPanel();
}
}
else{
createFeedbackPanel();
}
});
}
function createFeedbackPanel(){
var win = window.open('feedback-panel.html', 'Feedback', 'width=935, height=675');
console.log(win); // logs full object as expected
chrome.storage.local.set({"feedbackPanel": win});
}
$('#some-button').click(openFeedbackPanel());
So, since this doesnt work:
How can I check if a popup window is already open from a non-parent window (one that did not open the popup)?
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,
You can also use the messaging system. This is an example I use for an extension's options. Call a function like this in the onClick for the button:
// send message to the option tab to focus it.
// if not found, create it
showOptionsTab: function() {
chrome.runtime.sendMessage({window: 'highlight'}, null, function(response) {
if (!response) {
// no one listening
chrome.tabs.create({url: '../html/options.html'});
}
});
},
And in your window/tab listen for the message like this
// listen for request to make ourselves highlighted
chrome.runtime.onMessage.addListener(t.onMessage);
...
// message: highlight ourselves and tell the sender we are here
t.onMessage = function(request, sender, response) {
if (request.window === 'highlight') {
chrome.tabs.getCurrent(function(t) {
chrome.tabs.update(t.id, {'highlighted': true});
});
response(JSON.stringify({message: 'OK'}));
}
};
One advantage of this method is it does not need the tabs permission.
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.
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();
}
}
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);
}
}