如何在新窗口中打开SAME页面,并与它们交叉JavaScript?

时间:2013-01-26 18:27:59

标签: javascript jquery dom window

我知道这是一个不寻常的问题。不,这不是一个错字。

我想在新窗口中打开同一页。有了这个新窗口,我想交叉javascript操作。意味着新窗口可以操纵旧窗口,反之亦然;从JavaScript(可以是jQuery)到完整的DOM。加上同步滚动

非常感谢!

2 个答案:

答案 0 :(得分:2)

下面:

// you use a regular window.open()
var w = window.open(window.location.href);
// the variable w now contains a reference to the newly opened window.

// from the newly opened window use window.opener : 
window.opener.alert("Called in parent window.");

答案 1 :(得分:1)

确定。终于找到了我所有头痛的真正答案。

我将解释在同一页面中父窗口和子窗口之间进行通信的最佳方式。可以针对不同页面(但来自同一主机)调整此逻辑。

使用:JavaScript + PHP

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
 <!--
 var window_ = null;
 var n = false;  //Variable n


<?php
    if(isset($_GET['n'])) //This will make the n variable go true if 
    //'?n' (without quotes) is found in the URL. Meaning, that the child exists
    {
        print('n = true;');
    }
?>


function openChildWindow()  //function which opens the child window. 
 {
  if (window_ != null) //This will check if the window is opened
  {
   if  ( window_.closed == false ) //if the window is opened, a.k.a is not closed
    window_.close(); // close the window
   window_ = null; // and nullify the variable, so it can be reopened if desired. 
  }

else
{
 window_ = window.open(window.location.href+'?n'); //This is the tricky part. 

/*Assign the window.location.href so it will open a new window with the same page 
BUT assign it the n variable, so the GET function will receive it, and set n to true.
*/

 window_.focus();
}
 // Need to call on a delay to allow
  // the child window to fully load...

 }

function callChildWindowFunction()
 {
  if ( (window_ != null) && (window_.closed == false) )
        {
        window_.shout('bearl');
        }


 }
 // -->

 function shout(val)
 {
     alert(val);
 }
 $(document).ready(function(e) { 
 /*Wait for the document to load, so you can check if the variable n is true,
 which means that **IT IS** the child window and it has been loaded completely. Meaning 
 that you can finally manipulate the child window with the parent document*/
    if(n == true)
 {
    window.opener.callChildWindowFunction(); //Call the parent telling it "I'm ready" 
 } 
});

 </script>


 <input type="button" value="Blear" onClick="openChildWindow();">

现在,解释代码。

这里的技巧是创建一个“人工切换”,告诉浏览器哪个页面是父页面,哪个页面是孩子。

执行此操作的人是变量 n 。通过赋予它一个假值,我告诉它是已经加载的父母,而不是孩子。

打开子窗口时(请参阅openChildWindow()函数),它将验证窗口是否已打开,以便根据情况关闭/打开它。

如果窗口打开,它将分配EXACT URL和?n变量,这将帮助PHP识别它是子窗口。

在打开页面的底部,文档加载后,n将为true,因此它将通过 window.opener 函数调用父项。这将触发一个函数,它将触发子函数(shout(val))。

您也可以使用两个不同的页面执行此操作。

创建一个页面“A.html”和一个页面“B.html”(没有引号)。

页面A将获得以下代码:

<script type="text/javascript">
 <!--
 var window_ = null;
function openChildWindow() 
 {
  if ( (window_ != null))
  {
   if  ( window_.closed == false )
    window_.close();
   window_ = null;
  }

 var windowUrl = 'B.html';

 window_ = window.open(windowUrl);

 window_.focus();
 }

function callChildWindowFunction()
 {
  if ( (window_ != null) && (window_.closed == false) )
        {
        window_.shout('bearl');
        }
}
 // -->
 </script>


 <input type="button" value="Blear" onClick="openChildWindow();">

Page B将获得此

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
 function shout(val)
 {
     alert(val);
 }
 $(document).ready(function(e) {
 {
    window.opener.callChildWindowFunction(); 
 } 
});

 </script>

请注意,机制是相同的。唯一不需要“人工切换”的因为页面不同。

只需指定要创建$(document).ready()函数的页面B,以验证它是否已完全加载,并通过window.opener函数调用父级,以便它可以开始调用子级。

如果您找到了更好的方法,请继续向我展示。 setTimeOut,setInterval函数不起作用,因为代码在时间之前预先执行(Opera)。此外,这将确保文档100%加载,如果用户具有慢速Internet连接,代码将不会中断。