Dynamically create an iframe and attach onload event to it

后端 未结 4 2206
隐瞒了意图╮
隐瞒了意图╮ 2021-02-08 05:20

I have created a iframe dynamically and added a src attribute to it. Then I have appended this iframe to body of the page. Know I want to attach an onload

4条回答
  •  半阙折子戏
    2021-02-08 05:46

    I just tried this and it worked in Chrome:

    var iframe = document.createElement('iframe');
    // append iframe to DOM however you like then:
    iframe.contentWindow.parent.location.href // properly gives parent window's href.
    

    W3school says contentWindow is supported in all major browsers.

提交回复
热议问题