Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use contentwindow

Shulou Source: shulou.com Published: 2022-06-03 11:01:45 10月15日 Update

Editor to share with you how to use contentwindow, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

The contentDocument property can return documents in iframe as HTML objects, and the returned objects can be processed through all the standard DOM methods.

Definition and usage of contentwindow

Syntax: frameObject.contentWindow, or iframeObject.contentWindow (not a jquery object)

When nesting pages with iframe, if the parent page wants to get the content in the child page, you can use contentWindow or contentDocument, the difference is as follows:

1. ContentWindow this is a read-only property that returns the window object of the specified iframe. Although it is not part of the standard, all major browsers support it.

2. ContentDocumentFirefox supports it, but IE6,IE7 does not support it. IE8 starts to support it, so you need to access document.frames ['Jacks mainframe'] .document.

Compatible to get document objects:

VargetIFrameDoc=function () {

Variobj=document.createElement ("iframe")

Document.getElementsByTagName ("body") [0] .appendChild (iobj)

Returniobj.contentDocument | | iobj.contentWindow.document

}

The basic use of contentwindow:

1. Document.getElementById ("myiframe") .contentWindow. After you get the iframe object, you can get the window object of the iframe containing the page through contentWindow, and then you can access the page elements normally.

2. $("# myiframe") [0] .contentWindow. The jquery selector gets the iframe. Convert the jquery object to a DOM object first, or use the get () method to convert it.

3. $("# myiframe") [0] .contentWindow. $("# dd") .val (), you can get the window object of iframe and then use the jquery selector for page operation.

4. $("# myiframe") [0] .contentWindow.username = "zhangsan"; you can pass parameters to the iframe page in this way, and the value can be obtained in the iframe page window.username. Username is a custom global variable.

5. On the iframe page, you can get the window of the main page through parent, and then you can access the elements of the parent page normally.

6. Parent.$ ("# frame_A") [0] .contentWindow.document.getElmentById ("# frame_B"). If you call between iframe pages of the same level, you need to get the father's window first, and then call the iframe of the same level to get the window to operate.

The above is all the contents of this article "how to use contentwindow". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Pages objects support content can be passed articles elements siblings properties methods standards parents selections less mainstream home pages between global parameters variables Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno MySQL Shulou Tech Info Xiaomi macOS