Javascript - If pop-up (by name) is open? - javascript

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>

Related

How to detect window close event that was opened using window.open()

I am trying to detect the window close event that I opened using window.open() in javascript. But for some reason, it doesn't seem to work.
Here is my code:
<html>
<head>
<script>
var clicktest = function() {
var newwindow = window.open("https://www.google.com",'myPopupwindow', "height=640,width=960,toolbar=no,menubar=no,scrollbars=no,location=no,status=no");
newwindow.addEventListener("beforeunload", function (e) {
console.log('hey');
});
}
</script>
</head>
<body>
<button onclick="clicktest()">hey</button>
</body>
</html>
I also tried using
newwindow.onbeforeunload = function () {
console.log('hey');
}
instead of window.addeventlistener(), but both didn't work and I did try using window instead of newwindow, still, it didn't work.
For cross-origin documents, the only solution is to poll the .closed property of the popup Window object.
But that is a very ugly thing to do, so please have a second though about why you need that.
To limit the ugliness, you can power your polling using battery friendly requestAnimationFrame:
const popup = window.open('https://google.com');
waitForClose(popup, e=>console.log('closed'));
function waitForClose(win, cb) {
function poll() {
if(win.closed) {
cb();
}
else {
requestAnimationFrame(poll);
}
}
poll();
}
As a fiddle since StackSnippet's iframes don't allow popups.

find out a specific url in child window adressbar

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 to activate full screen mode?

I am trying to activate full screen mode using Javascript but it is not working.
Please note that console logs inside the function (on line 4 and 11) are printing correct values but full screen mode is not appearing.
what might be the issue? Here's my code.
function fullscreen()
{
var _element = document.getElementById('BookMainDiv');
console.log(_element);
if (_element.mozRequestFullScreen)
{
_element.mozRequestFullScreen();
}
else if(_element.webkitRequestFullScreen)
{
console.log("aaa");
_element.webkitRequestFullScreen();
}
}
HTML
<body>
<div id="BookMainDiv" style="width:500px;height:500px;background-color:yellow">
</div>
<input type="button" style="height:20%;width:20%" onclick="fullscreen()">
</body>
p.s. I am using chrome 31 on windows 8
This should work..
<html>
<head>
<script>
function fullscreen()
{
var fullscrn = document.getElementById("BookMainDiv");
req= fullscrn.requestFullScreen || fullscrn.webkitRequestFullScreen || fullscrn.mozRequestFullScreen;
req.call(fullscrn);
}
</script>
</head>
<body>
<div id="BookMainDiv" style="width:500px;height:500px;background-color:yellow">
</div>
<input type="button" style="height:20%;width:20%" onclick="fullscreen()">
</body>
</html>
P.S:Your code is working fine in my system , (yes, browser is chrome itself)
It happens that the requesFullScreen (webkitRequestFullScreen) does not work in Chrome when developper tools are open and you have break points in the javascript!
Close it and you will see it will work!
----------------------------- EDITED ------------------
Be sure your document declaration is this one:
<!DOCTYPE html>
And try this code :
function FullScreen() {
var element = document.getElementById("BookMainDiv");
if (element.requestFullScreen) {
element.requestFullScreen();
}
if (element.webkitRequestFullScreen) {
element.webkitRequestFullScreen();
}
if (element.mozRequestFullScreen) {
element.mozRequestFullScreen();
}
}

Javascript: how to open a page then wait for few seconds open another page in the same window

I tried to open an url in a window then wait for few seconds and opened another url in the same window. But the script doesn't work. When run it gives a blank window. I am new in Javascript. Can someone please help me?
I want to run it in Google Chrome as well.
The script is as follows:
my_window=window.open("","mywindow");
my_window.location="http://www.yahoo.com";
sleep(10000);
my_window.location="http://www.youtube.com";
sleep(10000);
my_window.close();
function sleep(delay)
{
var start = new Date().getTime();
while (new Date().getTime() < start + delay);
}
You could give this a try:
<script type="text/javascript">
function def()
{
my_window.location="http://www.yahoo.com";
setTimeout("abc()", 3000);
}
function abc()
{
alert("Delayed 3 seconds");
my_window.location="http://www.youtube.com";
}
</script>
In general, it is inadvisable to use blocking loops in javascript.
In your case, you would want to use something like setTimeout or setInterval.
This code should work:
var win = window.open("http://foo.com");
setTimeout(function(){
win.location = "http://bar.com";
setTimeout(function(){
win.close();
}, 10000);
}, 10000);
Tested and this one works but the popup blocker appears
<!DOCTYPE html>
<html>
<head>
<script>
function open_win()
{
setTimeout("go('http://www.yahoo.com')", 5000);
setTimeout("go('http://www.youtube.com')", 10000);
}
function go(url){
window.open(url);
}
</script>
</head>
<body>
<form>
<input type="button" value="Open Win" onclick="open_win()">
</form>
</body>
</html>
UPDATED
I wrote the following HTML and it worked fine for me showing what is required in your post:
<html>
<head>
<script language="JavaScript" type="text/javascript">
var my_window;
function OpenWin()
{
my_window=window.open("http://www.yahoo.com", "_blank", "resizable=yes, scrollbars=yes, titlebar=yes, width=1000, height=800, top=10, left=10");
setTimeout("GoUrl('http://www.youtube.com')", 10000);
}
function GoUrl(Url)
{
my_window.location=Url;
}
</script>
</head>
<body>
<button onclick="OpenWin()">Open Window</button>
</body>
</html>

How do I check that opened window is closed with javascript?

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

Categories

Resources