Getting parents document from iFrame

前端 未结 1 1875
悲哀的现实
悲哀的现实 2020-12-06 06:28

I have seen some elegant solutions on fetching the parents document from iframe - like e.g. this one. However, I can\'t make it work - and I didn\'t think this was possible

相关标签:
1条回答
  • 2020-12-06 06:56

    It's still possible to access the parent from within a frame provided that the domains match.

    For example, have a look at these fiddles:

    • Frame host: fiddle.jshell.net, parent host: fiddle.net Does not match = failure
      Test #1: http://jsfiddle.net/nrRQg/1/
    • Frame host: jsfiddle.net, parent host: jsfiddle.net **Matches = success*
      Test #2: http://jsfiddle.net/nrRQg/1/show/

    You can access the parent through:

    window.parent
    parent
    top          //If the parent is the top-level document
    window.top
    

    The variables parent and top can be overwritten (usually not intended). It's safer to use window.parent to be more safe. Alternatively, you can replace window by document.defaultView.

    0 讨论(0)
提交回复
热议问题