开发者

Javascript parent and child window functions

function myFunction(){

myWin = open("","","width=200,height=200");

with(myWin.document){
  open();
  write("<HTML><HEAD><TITLE>Child Window</TITLE>");
  write("<SCRIPT>function myTest(){");
  write("alert('This function开发者_JAVA百科 is defined in the child window ");
  write("and is called from the parent window.'); this.focus();}");
  write("</SCRIPT></HEAD><BODY><H3>Child Window</H3><HR>");
  write("<FORM><INPUT TYPE='button' VALUE='parent window function' ");

        write("onClick='opener.winFunction();'>");                                            
   write("<P><INPUT TYPE='button' VALUE='close window' ");
   write("onClick='window.close();'>");
   write("</FORM></BODY></HTML>");
   close();
 }
}

function winFunction(){
    alert("This function is defined in the parent window\n" +
          "and is called from the child window.");
    myWin.focus();
}

//-->

To explain further what my initial query was, the code above, should open the child window (myWin) with the second button, the 'Open child window' button without the need to open the window with the first button or do anything else. It should simply call the myWin.myTest()function The child window will open when the second button is pressed but needs to have the child window open first (first button push) before it'll work. This is not the intended purpose, the 'Open child window' button should work without anything else needing to be done. For some reason the parent window isn't communicating with the myWin window and myTest function. It's not homework, it's part of a certification course lab and is coded in the manner I have been shown to understand as correct. DTD isn't included as the focus is the JavaScript. I code correctly with regards to that and other W3C requirements.


I redid the code slightly nicer. It works as intended here. Apart from referencing the correct window object directly it isn't that much different.

The code still looks ugly, create new windows with urls rather then like this.

http://jsfiddle.net/B8rDN/24/


Works fine (although i escaped the / of the closing tags in the string and made the \/)

demo: http://www.jsfiddle.net/4FBZv/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜