Access a variable of iframe from parent

后端 未结 6 926
野趣味
野趣味 2020-12-03 09:35

script of iframe


script of parent window



        
6条回答
  •  时光说笑
    2020-12-03 10:04

    One method that has always worked reliably for me is for the iFrame to give its parent a reference to its own window when it first loads. The parent can then access all the variables through that reference. This does require that the parent is loaded before the iFrame, but for me that is usually the case.

    So in the parent

    var iFrameWin;
    

    Then in the iFrame at some point after it has loaded and settled down

    parent.iFrameWin = window;  //parent now has a ref to the iframe's window
    

    Then, in the parent when it wants a global var contents from the iFrame

    alert(iFrameWin.ivar);  // shows value if the global 'ivar' in the iFrame
    

提交回复
热议问题