Need to close all new window opened by my application - javascript

I have tried to close the new window by Window.close() this works for current tab close but I need to close all the new tabs opened by my application
var newwindow=null;
function NewWindow(mypage,myPagename)
{
params = 'width='+screen.width;
params += ', height='+screen.height;
params += ', top=0, left=0';
params +=', scrollbars=yes,menubar=yes,toolbar=yes';
newwindow=window.open(mypage,myPagename, params);
if (window.focus)
{
newwindow.focus()
}
return false;
}
Shall we close the new window by having their name , if yes means how ???

You can assign window.open() to a variable and then addEventListener to the window variable. Something like this:
var testWindow = window.open("enter/your/url");
testWindow.addEventListener('loadstart', yourListenerFunction);
Now when you want to close the window, you need to do this:
testWindow.removeEventListener('loadstart', yourListenerFunction);
testWindow.close();
removeEventListener() method removes the event listener previously registered with addEventListener().

Related

open window and run function on that window

I'd like to open a new window, this window has a list of objects, and these objects should be filtered based on a selection from the previous window. I figured I can filter the list through a function, but how do I run said function?
This is what I am able to do:
var popup = window.open('pageURL');
$(popup.document).ready(function() {
// this is where function should be
popup.alert('HelloWorld');
});
But how do I change the alert to a function?
If I have a function on my other app , function test() { alert('HelloWorld'};
How do I run this function from my first app?
Swapping popup.alert('HelloWorld'); with popup.test(); did not work.
You need the reference to the window opened to call functions in the new window, like:
var oNewWindow = window.open("new.window.url", "mywindow");
oNewWindow.onload = function(){oNewWindow.window.newWindowFunction();};
I ended up with this solution
var popup = window.open('http://s234-0057/actiontracker/SiteAssets/Avvik/html/app.aspx');
var readyStateCheckInterval = setInterval(function() {
if (popup.document.readyState === "complete") {
clearInterval(readyStateCheckInterval);
popup.test();
}
}, 50);
Where I check if the popup window is ready, and when it is, cancel check and run function. Solution is from top answer on this question, by #this.lau_
You can write it like this:
function myFunction(){
alert('HelloWorld');
}
var popup = window.open('pageURL');
$(popup.document).ready(function() {
popup.eval(myFunction + "");
popup.myFunction();
});
myFunction in file that contains this code will run in page with pageURL address.

Opening two instances of InAppBrowser (_system and _blank) prevents events from triggering

We’re currently developing an app with cordova and the InAppBrowser plugin. We're trying to spawn two different IAB instances at the same time. One with the _system browser and another with the _blank option.
The problem we have is that once we open the instance of _system browser, it seems we lose the reference to the previous browser. For this reason, the close event never triggers on the _blank IAB after the _system browser is closed.
This is how the actual code looks like.
// Opening iab main window
var ref = window.open(global.chat_mediador, '_blank','location=no,toolbar=yes');
var handleEvents = function(event) {
// Closing the iab window
if (event.url.match('#close')) {
ref.close();
}
// Trigger custom event
if (event.url.match('#openccard')) {
window.open('https://www.test.example.url.com?customerID=' + event.customerId, '_system', 'location=yes');
}
}
// InAppBrowser events
// This events are duplicated because loadstop works on android and
// loadstart works on ios.
ref.addEventListener('loadstart', handleEvents, false);
ref.addEventListener('loadstop', handleEvents, false);
// Removing the dialog when we close the chat
ref.addEventListener('exit', function(event) {
generali.dialog.close();
}, false);
As you can see we open the first url within the application with the _blank option. Then if in the child application a button is pressed we want to open an instance of a browser in the _system browser.
We’ve tried (without luck) to:
Have a separate reference for the _system browser.
window.open(global.url_ficha + customerId, '_system','location=no');
var cardsRef = window.open(
'https://www.test.example.url.com?customerID=' + customerId,
'_system',
'location=yes'
);
Trigger a custom event outside the reference of the _blank browser
if (event.url.match('openccard')) {
var customerId = event.url.split('openccard-')[1];
var evt = document.createEvent("Event");
evt.initEvent("openccard",true,true);
evt.customerId = customerId;
document.dispatchEvent(evt);
}
Anyone has an idea of what's happening?
It seems that you need to initialize the IAB each time you do a new window.open() if you don't do that the event listeners don't work.
If I use that code it works like a charm.
window.openIAB = function(url, target, options) {
var self = this;
var ref = window.open(url, target, options);
var handleChildEvents = function(ev) {
if (ref != undefined) {
// Closing the iab window
if (ev.url.match('#close')) {
ref.close();
ref = undefined;
}
// Opening card url with system browser
if (ev.url.match('#openccard')) {
var customerId = ev.url.split('#openccard-')[1];
self.ref2 = self.openIAB(
'https://www.test.com?customerID=' + customerId,
'_system',
'location=yes'
);
}
} else {
console.log('InAppBrowser has no reference');
}
};
ref.addEventListener('loadstart', handleChildEvents);
ref.addEventListener('loadstop', handleChildEvents);
ref.addEventListener('loaderror', function(ev) {
console.log('error while loading page');
ref.close();
ref = undefined;
});
ref.addEventListener('exit', function(ev) {
dialog.close();
});
return ref;
};

Pop Up Blocker in Chrome and IE

Below is the piece of code I am using to open a link in a new window say "abc".
If the user again clicks on the same link, it should close and reopen the link in the same window "abc".
window.openOrFocus = function(url, "abc") {
if (!window.popups) {
window.popups = {};}
if (window.popups["abc"]){
var v=window.open("", "abc");
v.close();}
window.popups["abc"] = window.open(url, "abc");
}
But Now, say I click on the link, it opens the URL in a new window named "abc".
Now I go and close the window "abc". and go back and again click on the link.
That time it shows up the pop up blocker.
I am confused as to why this pop up blocker is coming when the I go and manually close the window and try to reopen by clicking on the link.
Happens both in IE as well as Chrome
Probably because you're calling window.open with a blank URL or repeatedly in that case.
You don't need your window.open("", "abc") call; instead, just use the window reference you already have:
window.openOrFocus = function(url, windowName) {
if (!window.popups) {
window.popups = {};
}
if (window.popups[windowName]){
window.popups[windowName].close();
}
window.popups[windowName] = window.open(url, windowName);
};
I would also listen for the unload event so you can remove your reference:
window.openOrFocus = function(url, windowName) {
if (!window.popups) {
window.popups = {};
}
if (window.popups[windowName]){
window.popups[windowName].close();
}
window.popups[windowName] = window.open(url, windowName);
window.popups[windowName].onunload = function() {
delete window.popups[windowName];
};
};
Side note: This is a syntax error:
window.openOrFocus = function(url, "abc") {
// --------------------------------^
I've replaced it with windowName in the code above.

Opening only one instance of url using window.open

I am working on an asp.net web page which has a hyperlink. when ever that hyperlink is clicked, a new browser window is opened using javascript window.open. I want that If user clicks this link multiple times, then only one window is opened and not multiple windows. I just want that window to be highlighted when user clicks that hyperlink multiple times. Do I need to use window.open to detect if the url is opened in any other tab of the browser ? Is there any jQuery plugin built in for this so that I can use it for browser compatibility.
Here is the hyperlink url:
<a onclick="addClick()" href="javascript:void(0)">
New</a>
and here is the code I am using:
function addClick() {
var ID = jQuery("#ID").val();
var PSSWD = jQuery("#PSSWD").val();
var ACCID = jQuery("#ACCID").val();
var PASSWDINT = jQuery("#PASSWDINT").val();
window.open("LoginAPI?ID=" + encodeURIComponent(ID) + "&PSSWD=" + encodeURIComponent(PSSWD) + "&ACCID=" + encodeURIComponent(ACCID) + "&PASSWDINT=" + encodeURIComponent(PASSWDINT) + "", "LoginAPI");
}
Please suggest.
Try
window.open("<url>", "<window name>");
This should always open in the same window. See reference.
HTML:
open window
var wins = {};
function openwindow(){
var url = this.href;
if(typeof wins[url] === 'undefined' || wins[url].closed)
wins[url] = window.open(url);
}
<script>
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. */
};
}
</script>
click here
Check the reference https://developer.mozilla.org/en-US/docs/Web/API/window.open
To open only one instance of a popup window in an HTML page, use the windowName parameter of the window.open method.
For example
window.open('http://www.abc.com')
will open a new window each time the user clicks the link containing the window.open code.
In constrast,
window.open('http://www.abc.com','abc')
will open only one instance of the window, no matter how many times users click the link.
you can also use focus function as used below
<script language="javascript" type="text/javascript">
<!--
function popitup(url) {
newwindow=window.open(url,'name','height=200,width=150');
if (window.focus) {newwindow.focus()}
if (!newwindow.closed) {newwindow.focus()}
return false;
}
// -->
</script>
Edit 1
<a onclick="return addClick()" href="javascript:void(0)">New</a>
and here is the code I am using:
function addClick() {
var ID = jQuery("#ID").val();
var PSSWD = jQuery("#PSSWD").val();
var ACCID = jQuery("#ACCID").val();
var PASSWDINT = jQuery("#PASSWDINT").val();
window.open("LoginAPI?ID=" + encodeURIComponent(ID) + "&PSSWD=" + encodeURIComponent(PSSWD) + "&ACCID=" + encodeURIComponent(ACCID) + "&PASSWDINT=" + encodeURIComponent(PASSWDINT) + "", "LoginAPI");
return false;
}

Windows.open - how do I handle when it's being closed?

By calling window.open I'm opening a new window with external website from my Sinatra application:
%a{:href=>"some_external_website.com", :target=>"blank", :onclick=>"popupWin = window.open(this.href, 'test', 'location,width=600,height=500,top=0'); popupWin.focus(); return false; window.open('')"}
I want to handle a moment when an user is closing a window. How can I do it? Of course, in javascript.
Add an onunload handler to the window. Using your current approach:
var popupWin = window.open(this.href, 'test', 'location,width=600,height=500,top=0');
popupWin.focus();
// add an onunload handler
popupWin.onunload = function () { ... };
return false;
window.open('') // this is dead code, unconditional return above...

Categories

Resources