2010-03-22 4 views
3

Je rencontre des problèmes pour savoir comment référencer les nouvelles fenêtres du navigateur après les avoir ouvertes. À titre d'exemple, si je crée 3 nouvelles fenêtres d'un principal (index.html):javascript ouvrir les références de fenêtres

var one = window.open('one.html', 'one',"top=10,left=10,width=100,height=100,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,resizable=no"); 

    var two = window.open('two.html', 'two',"top=100,left=10,width=100,height=100,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,resizable=no"); 

    var three = window.open('three.html', 'three',"top=200,left=10,width=100,height=100,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,resizable=no"); 
    two.focus(); 

Comment pourrais-je me concentrer sur programme (ou tout simplement se référer à) navigateur « trois » si le navigateur « deux » est actuellement au point?

Répondre

2

Je voudrais avoir un tableau de fenêtres enfants dans la fenêtre parente. Ensuite, pour chaque fenêtre enfant, ayez une fonction qui ajoute l'enfant au tableau childWindow du parent. De cette façon, vous pouvez avoir un nombre arbitraire de fenêtres enfants.

//In the 'Main' window 
var childWindows = new Array(); 

//In the child window 
function onload() 
{ 
    window.parent.childWindows.push(window); 

} 

window.attachEvent('onload', onload); 
//or 
window.load = onload 
//or with jQuery 
$(window).ready(onload) 

Régler la mise au point comme ceci:

//In the parent 

childwindows[i].focus(); 

//In a child 

parent.childwindows[i].focus(); 

//or to focus on next child from within a child 
var len = parent.childwindows.length; 
for (var i = 0; i < len; i++) 
{ 
    if (parent.childwindows[i] && parent.childwindows[i] == window) //you might want some other way to determine equality e.g. checking the title or location property. 
    { 
     var n = (i + 1) % childWindows.length; 
     childwindows[n].focus(); 
    } 

} 
+0

pour la "window.parent.childWindows.push (fenêtre);" je reçois toujours "Impossible d'appeler la méthode" push "de undefined" même si j'ai déclaré le tableau childWindows dans ma fenêtre parente – minimalpop

+0

Essayez window.opener.childWindows –

+0

Testé, cela fonctionne. –

Questions connexes