is there any way to detect that which popup window i have opened is closed or not?
<script type="text/javascript">
// global variable for subwindow reference
var newWindow;
// generate and fill the new window
function makeNewWindow() {
// make sure it isn't already opened
newWindow = window.open("http://www.google.com","sub","status,height=200,width=300");
}
function checkWindow() {
if(newWindow.closed){
document.write("Window has closed.");
}
}
</script>
<form>
<input type="button" value="Create New Window" onclick="makeNewWindow();" />
<script>
checkWindow();
</script>
</form>
I want that when i close the opened window. the function checkWindow() print on screen that "Window has closed." Please suggest me some helping code. thanks in advance
var win = window.open('http://www.google.com','google','width=800,height=600,status=0,toolbar=0');
var timer = setInterval(function() {
if(win.closed) {
clearInterval(timer);
alert('closed');
}
}, 1000);
Related
I am trying to get the reference of the newly opened tab and use it in the same function to close the tab and reopen,
for more details:
I want to create a Java script button
on the first click open a new tab
on the second click of the same button close the tab and reopen the same tab
var Window;
// Function that open the new Window
function windowOpen() {
Window = window.open( "https://www.google.com/", "_blank", "width=400, height=450");
// if (window.opener) {
// window.opener.announceWindow( window );
// }
}
// function announceWindow( win ) {
// window.close();
// }
<button onclick="windowOpen()" target=_blank>
Open Window
</button>
You could use something like this:
let google;
let open = false;
function toggleWindow() {
open = !open;
if (open) {
google = window.open("https://www.google.com/", "_blank", "width=400, height=450");
} else {
google.close();
}
}
You can do like this:
var Window = window.open('https://www.google.com');
function windowOpen() {
if(Window !== null){
alert("Window alreay open");
Window.close();
Window = null;
}else {
Window = window.open('https://www.google.com');
}
}
<button onclick="windowOpen()">
Open Window
</button>
open() function opens a new tab. It's name property has _blank value by default. But if you close the tab manually, then you have to refresh the page to work JS properly otherwise you will get alert "Window already open" even after the closing of the tab.
I'm trying to find a way to detect that child window has opened a specific URL ?
<html>
<head>
<script type="text/javascript">
function openWin()
{
var child=window.open("https://www.google.com/search?q=bing");
var timer = setInterval(checkChild, 100);
function checkChild() {
var urll = child.location;
if (urll.match(/bing.com.*/)) {
alert("bing opened")
}
if (child.closed) {
alert("Child window closed");
clearInterval(timer);
}
}
}
</script>
</head>
<body>
<input type="button" value="Open window" onclick="openWin()" />
</body>
</html>
User opens https://www.google.com/search?q=bing in child window and click on search result and go to bing.com.
How can i find out that child window's URL is LIKE to bing.com ?
is it possible generally to do that? or i'm wasting my time?
if it is imposible , is there any other way?
How can I close modal dialog(s) from main page after some time, or when session expires, using JavaScript or jQuery?
Dialog is opened using the following code :
var result = window.showModalDialog("test.aspx" ... );
Dialog must be closed when counter expires like this:
function Discount() {
leftSeconds = leftSeconds - 1;
try { document.getElementById('tbLeft').value = leftSeconds; } catch (ex) { }
if (leftSeconds <= 5) {
clearTimeout(t);
// code for closing modal dialog(s)
} else {
t = setTimeout("Discount()", 1000);
}
}
Modal dialog can be closed from himself, but it's not solution in my case.
While the modal dialog is open, javascript execution on the main page is stopped, because it is waiting for a return value (even though you may not want to return one, or do anything with what it returns).
You can check this with this little example. When you click the button, the page opens, and the timer stops updating. When you close the page, execution is resumed:
<!DOCTYPE html>
<html>
<head>
<script>
var t = 0;
function count() {
document.getElementById('div').innerHTML = ++t;
}
var timer = setInterval(count, 1000);
</script>
</head>
<body>
<div id='div'></div>
<button onclick="window.showModalDialog('http://www.google.es');">Open window</button>
</body>
</html>
So, if you want to close the window automatically, you need to do it from the new document itself. My advice? Implement your timer in the window.load event of your modal page, so it can close itself after the desired time.
window.onload = function() {
setTimeout(function() { window.close(); }, 60000); //close window after 1 minute.
};
hi please check the below code i am trying to open tab with name
and after that try to close that tab but unable to close
or else can anyone tell how we can close opened all tab (url1,url2) from parent tab (url) while close parent tab
error in firebug is my_window is undefined
<form name="submitForm1" target="my_window" method="POST" action="http://localhost:8080/ADDMIBREP/">
<input type="hidden" name="uname" value="uname">
ADDMIBREPORT
</form>
remove
</body>
<script>
function closepopup()
{
alert("hi");
if(false == my_window.closed)
{
my_window.close ();
}
else
{
alert('Window already closed!');
}
} <script>
remove
OR
var my_first_window = window.open('test/test.html');
var my_second_window = window.open('test/test1.html');
window.onunload = function() {
if(!my_first_window.closed)
my_first_window.close();
if(!my_second_window.closed)
my_second_window.close();
}
Heres an example of what your trying to do... http://jsfiddle.net/KjBj3/7/
var win = window.open("", "win","width=200,height=100");
win.document.write('This is myWindow! Click to close');
win.focus();
win.opener.document.write('<p>This is the source window!</p>');
How can you check to see if a pop-up is already open strictly by the original pop-up's name, and not URL, etc.
The pop-up is opened via window.open().
Keep the handle to the window:
var popup = window.open( URL, name, features )
So later you can check whether it's closed by using it's "closed" property.
if (popup.closed) {
// closed
}
else {
// still open
}
You can see it working here: http://www.javascripter.net/faq/windowclosed.htm
EDIT
You should be able to do just what Cheery said, but if you would like more detail, I tested this, and it works:
<html>
<head>
<script type="text/javascript">
var popup;
function openPopup() {
popup = window.open("http://www.stackoverflow.com", "so", "location=1,status=1,scrollbars=1,width=300,height=300");
}
</script>
</head>
<body>
<button onclick="openPopup()">open popup</button>
<button onclick="checkIfPopupIsOpen()">check for popup</button>
<script type="text/javascript">
function checkIfPopupIsOpen() {
if (popup.closed) {
alert("it's closed");
}
else {
alert("it's still open");
}
}
</script>
</body>
</html>