Open a new tab if it is not already open - javascript

$('#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. */
};
}

Related

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

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.

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

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

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

How can we know when pop-up window url is loaded (window.open)?

I need to change the URL of the page in the pop-up as soon as it completes loading ( I am using window.open function call).
Is there anyway I can find out when the page in pop-up has completed loading in the parent window? I cannot change anything in the page I am opening in pop-up, because it belongs to another website.
window.open returns a reference to that window's window object.
If the opened window is pointing to a URL on the same domain as the window opener, then it will have full access to that object just as it does it's own window object.
var w = window.open(url);
// If the window opened successfully (e.g: not blocked)
if ( w ) {
w.onload = function() {
// Do stuff
};
}
The same origin policy applies here. If the URL of the opened window is on a different domain, then the opener will not have access to members of that window's reference.
this seems pretty self-explanatory, but tell me if you need more explanation (or if it doesn't work, though it should...)
var winObj = window.open( /* lot of params */ );
if( !winObj )
{
/* pop-up blocked! */
}
else
{
winObj.onload = function( ){ /* do stuff here */ };
}

Categories