2013-02-23 84 views
0

您好我想在子窗口調用父窗口功能的JavaScript

運行從父窗口的功能,但下面的代碼不起作用

的index.php

//set status message 
function setStatus($html){ 
    $("#data").html($html); 
    } 

//twitter login page handler 
function twitter(){ 
    win = window.open('twit.php','win',"width=882,height=750,toolbar=0"); 
    setStatus('proccess...'); 
    } 

//close window 
function closeWin(){ 
    win.close(); 
    } 

//open new window 
$(".twitterBtn").click(twitter); 

強烈抵制。 php

<script language="javascript" type="text/javascript"> 
window.opener.setStatus('Login with Twitter!'); 
window.opener.closeWin(); 
</script> 

我的問題與window.opener,它不工作

請建議的方式,將在所有的瀏覽器,不與其他代碼

衝突謝謝

+0

https://developer.mozilla.org/de/docs/DOM/window.postMessage – cIph3r 2013-02-23 12:51:51

回答

1

好了,過了一段時間,但我有一個工作的例子。

我使用postMessage進行信息交換,但是,它只是讓孩子知道它的父母..但我將這個例子擴展到了父 - 母聊天。好玩:

<html> 
<head> 
    <script> 
     window.addEventListener("load",init,false); 
     window.addEventListener("message",message,false); //register postMessages 

     //we need this because master(original) and slave(=window.open) must do different actions 
     var isSlave=window.location.href.indexOf("slave=true")>0 

     //the other-window (in the slave, this is undefined, until the handshake is complete 
     var popupwindow; 
     if (!isSlave){ 
      popupwindow=window.open(window.location.href+"?slave=true", "_blank",'toolbar=0,location=0,menubar=0'); 
     } 

     // this is called from the child window, after handshake 
     function test(){ 
      var listElem = document.createElement('li'); 
      listElem.innerHTML="handshake succesful!!!!!"; 
      document.querySelector("ul").appendChild(listElem); 
     } 

     function init(){ 
      document.querySelector("button").addEventListener("click",function(){ 
         ////popupwindow.test(); //note this works only if same origin policy allows this, else use postMessages 

       //here we send the message to the other window 
       popupwindow.postMessage(document.querySelector("#inp").value,"*"); 
      },false); 


      //the timetrigger is needed, because opening and initializing the new window takes time, then we perform a handshake to let the new window know his origin 
      setTimeout(function(){ 
      if(!isSlave) 
       popupwindow.postMessage("handshake successful","*");//handsake needed for bidirectional conneciton 
      },200); 


     } 


     //called when receiving a message 
     function message(data){ 

      //needed for bidirectional message 
      if (!popupwindow){ 
       popupwindow=data.source; 
       //popupwindow.postMessage("handshake successful","*"); //send the handshake back to the origin (notification purpose only) //instead, we do test() 
       popupwindow.test(); 
      } 

      //display the message 
      var listElem = document.createElement('li'); 
      listElem.innerHTML=data.data; 
      document.querySelector("ul").appendChild(listElem); 


     } 
    </script> 
</head> 

<body> 
    <button>say hello</button> 
    <input type="text" id="inp" /> 
    <ul></ul> 
</body> 
</html> 
+0

@ clph3r請解釋這種方法可以從一個子窗口信息呈現給父窗口移動 如何你是否處理不同的頁面? – 2013-02-23 15:23:54

+0

喜歡討論服務,當你想登錄併發送評論與推特賬號 – 2013-02-23 15:29:11

+0

@alismith此代碼實現父窗口和子窗口之間的聊天。試試吧.. 因此你可以在這兩個窗口之間發送任何信息。 – cIph3r 2013-02-23 15:36:16