如何在新窗口上打开同一页面,并将JavaScript与之交叉?

如何在新窗口上打开同一页面,并将JavaScript与之交叉?,javascript,jquery,dom,window,Javascript,Jquery,Dom,Window,我知道这是一个不寻常的问题。不,这不是打字错误 我希望在新窗口上打开同一页。有了这个新窗口,我想进行跨javascript操作。这意味着新窗口可以操作旧窗口,反之亦然;从JavaScript(可以是jQuery)到完整的DOM。加同步滚动 非常感谢 这里: // you use a regular window.open() var w = window.open(window.location.href); // the variable w now contains a reference

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

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

非常感谢

这里:

// 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.");
在这里:


嗯。终于找到了我所有头痛问题的真正答案

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

使用: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();">

函数(val)
{
警报(val);
}
$(文件).ready(函数(e){
/*等待文档加载,以便检查变量n是否为true,
这意味着**它是**子窗口,并且它已经被完全加载。意思是
最终可以使用父文档操纵子窗口*/
如果(n==true)
{
window.opener.callChildWindowFunction();//调用父对象,告诉它“我准备好了”
} 
});
现在,解释代码

这里的技巧是创建一个“人工开关”,它将告诉浏览器哪个页面是父页面,哪个页面是子页面

执行此操作的是变量n。通过给它赋一个假值,我告诉它加载的是父对象,而不是子对象

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

如果窗口被打开,它将分配准确的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();">

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>

函数(val)
{
警报(val);
}
$(文档).ready(函数(e){
{
window.opener.callChildWindowFunction();
} 
});
请注意,这些机制是相同的。唯一不需要“人工切换”的是,因为页面不同

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


如果你找到了更好的方法,那就告诉我吧。setTimeOut、setInterval函数不起作用,因为在超时之前预执行的代码(Opera)。此外,这将确保文档已100%加载,并且如果用户的Internet连接速度较慢,代码不会中断。

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

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

使用: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();">

函数(val)
{
警报(val);
}
$(文件).ready(函数(e){
/*等待文档加载,以便检查变量n是否为true,
这意味着**它是**子窗口,并且它已经被完全加载。意思是
最终可以使用父文档操纵子窗口*/
如果(n==true)
{
window.opener.callChildWindowFunction();//调用父对象,告诉它“我准备好了”
} 
});
现在,解释代码

这里的技巧是创建一个“人工开关”,它将告诉浏览器哪个页面是父页面,哪个页面是子页面

执行此操作的是变量n。通过给它赋一个假值,我告诉它加载的是父对象,而不是子对象

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

如果窗口被打开,它将分配准确的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();">

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>

函数(val)
{
警报(val);
}
$(文档).ready(函数(e){
{
window.opener.callChildWindowFunction();
} 
});
请注意,这些机制是相同的。唯一不需要“人工切换”的是,因为页面不同

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


如果你找到了更好的方法,那就告诉我吧。setTimeOut、setInterval函数不起作用,因为在超时之前预执行的代码(Opera)。此外,这将确保文档已100%加载,并且如果用户的Internet连接速度较慢,代码不会中断。

var new\u window=window.open(location.href):。因为两个医生