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();
}
}
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. */
};
}
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.
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);
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);
}
}
I have parent browser window P . on clicking a button a new browser window WIN-A is opened.
then again pressing the same button it should read the title of the WIN-A window and open WIN-B
how to achieve this using Javascript?
Thanks in advance
Given:
var myWindow = open("foo.bar");
Old method: Change the window object's name property:
myWindow.name = "...";
// in foo.bar:
setInterval(someFunctionToCheckForChangesInName, 100);
HTML5 method: Call the window object's postMessage method:
myWindow.postMessage("...", "*");
// in foo.bar:
(window.addEventListener || window.attachEvent)(
(window.attachEvent && "on" || "") + "message", function (evt) {
var data = evt.data; // this is the data sent to the window
// do stuff with data
},
false);
A similar question was just asked recently:
Stack Overflow question: Quickest way to pass data to a popup window I created using window.open()?
Using that as a base (and provided Parent and WIN-A are on the same domain):
// Put outside click handler
var winA = null;
// Put inside click handler
if(!winA){
// Store the return of the `open` command in a variable
var winA = window.open('http://www.mydomain.com/page1');
} else {
var title = winA.document.title;
// Do something with title
var winB = window.open('http://www.mydomain.com/page2');
}
When you call window.open, it returns a reference to the newly opened window. You can store the references to the windows you open in an array, and then iterate that array when you need to open another new window.
Gabriel