HighDots Forums  

window.opener and looping

Javascript JavaScript language (comp.lang.javascript)


Discuss window.opener and looping in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Andrew Poulos
 
Posts: n/a

Default window.opener and looping - 11-03-2009 , 05:13 AM






I've written some code that looks like this:

function search(win){
/*
* search stuff here
* if you find the object return a reference to it
*/
if (win.opener && !win.opener.closed) return search(win.opener);
return null;
};

var foundObj = search(window);


It looks for an "known" object in the current window. If it can't find
it, it tries the window opener. And if the window opener has an opener
it will try that... and so on.

What is happening is that, if the object can't be found, I'm getting an
endless loop as it keeps feeding window.opener to 'search'.

If the opener itself does not have an opener I would expect the function
to not be called again.

Why does win.opener continue to be window.opener?

Andrew Poulos

Reply With Quote
  #2  
Old   
SAM
 
Posts: n/a

Default Re: window.opener and looping - 11-03-2009 , 07:03 AM






Le 11/3/09 11:13 AM, Andrew Poulos a écrit :
Quote:
I've written some code that looks like this:

function search(win){
/*
* search stuff here
* if you find the object return a reference to it
*/
if (win.opener && !win.opener.closed) return search(win.opener);
return null;
};

var foundObj = search(window);
(...)
What is happening is that, if the object can't be found, I'm getting an
endless loop as it keeps feeding window.opener to 'search'.

If the opener itself does not have an opener I would expect the function
to not be called again.

Why does win.opener continue to be window.opener?
With that (with no self loop) :

function searchy(win){
win = win.opener;
if(win) while (win.opener) win = win.opener;
return win;
};

and :
<button id="but"
onclick="alert(searchy(window).document.title);sea rchy(window).focus();">test</button>

If no PopUp (or intermediate closed PopUp) I get an error :
Erreur : searchy(window) is null
Ligne : 1
(the line where begins the JS is : 30)

changing :
while (win.opener)
in :
while (win.opener && !win.opener.closed)
changes nothing.


Don't know if that could help you ?




That below works better
(if an intermediate popup is closed the grand mother is no more reached)

<title>Pop 0</title>
<script type="text/javascript">
function searchy(win){
win = win.opener;
// alert(win);
var i=0;
if(win) while (win.opener) {
i++;
win = win.opener;
alert(i+' : '+win);
}
return win;
}
function report(w) {
w = searchy(w);
if(w)
{
alert(w.document.title);
w.focus();
}
else alert('no opener found');
}
function pop(t) {
window.open('pops_'+t+'.htm','w_'+t,'width=300,hei ght=150,left='+t*20);
}
</script>
</head>
<body>
<h1>POPUP 0</h1>
<div>
<p><a href="javascriptop(1);">popup</a></p>
<p><button id="but" onclick="report(window)">test</button>
</div>


--
sm

Reply With Quote
  #3  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: window.opener and looping - 11-04-2009 , 01:06 PM



Andrew Poulos wrote:

Quote:
I've written some code that looks like this:

function search(win){
/*
* search stuff here
* if you find the object return a reference to it
*/
if (win.opener && !win.opener.closed) return search(win.opener);
return null;
};

var foundObj = search(window);


It looks for an "known" object in the current window. If it can't find
it, it tries the window opener.
No, it does not.

Quote:
[...]
What is happening is that, if the object can't be found, I'm getting an
endless loop as it keeps feeding window.opener to 'search'.
WFM. Post the complete relevant code.


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7 (AT) news (DOT) demon.co.uk>

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.