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?
Related
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.
i am currently having problem including image in frame from another file, what i am doing is creating a html page with two frame having code :
<html>
<frameset cols="20%,80%">
<frame src="aa.html" id="1">
<frame src="" id="2">
</frameset>
</html>
Then, in aa.html there are three links and on clicking the first link an image should open in frame with id="2".
aa.html
<html>
<head>
<link rel="import" href="cook.html">
<script>
function fun1()
{
document.getElementById('2').src="b.html";
}
</script>
</head>
<body>
Home1
Hom2
Home3
</body>
</html>
then b.html shows image in the frame,like this :
<html>
<head>
</head>
<body>
<img src="ima.png"/>
</body>
</html>
But this does not work.thanks in advance.
You can't do what you want in this way.
This function:
function fun1() {
document.getElementById('2').src="b.html";
}
is working IN IFRAME, and not in MAIN WINDOW.
Try look at window.postMessage methods.
UPDATE working with window.postMessage look like that:
1) in child window will be function
function fun1() {
window.postMessage(JSON.stringify({"id":2,"src":"b.html"}),'http://parent.window.com');
}
2) in parent window, you should listen for this event, so add on start of page:
window.addEventListener("message", receiveMessage, false);
function receiveMessage( event )
{
if (event.origin !== "http://example.org:8080")
return;
try{
var data = JSON.parse(event.data);
if( data.id != undefined && data.src != undefined ){
document.getElementById(data.id).src = data.src;
}
} catch( exception ){ console.log( exception);}
}
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 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>
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);