Can javascript running inside an iframe affect the main page?

前端 未结 4 632
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-02 02:18

Partial Code:

My below code pulls a query from my DB and then uses inner.HTML = to display the data inside a div. It works fine in it original use....

Howeve

相关标签:
4条回答
  • 2020-12-02 02:29

    You can do it! Read here for more info

    You can affect the document with contains the iframe by setting and getting variables from the window element:

    // normally you use...
    var whatever = "value";
    
    // use the window object instead, which is shared 
    // between the iframe and the parent
    window.whatever = "value";
    

    The other thing you should know is that you can access the main document via the parent object

    inside the iframe you can use...

    parent.someattr;
    
    // or try this
    parent.getElementById('some_element');
    
    0 讨论(0)
  • 2020-12-02 02:35

    I think what you want is:

    parent.getElementById('posts').innerHTML = 'why doth this faileth?';
    
    0 讨论(0)
  • 2020-12-02 02:36

    The part in the iframe isn't considered the same document.

    0 讨论(0)
  • 2020-12-02 02:50

    you have to use parent.

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