Resizing an iframe based on content

后端 未结 21 2322
南方客
南方客 2020-11-21 07:40

I am working on an iGoogle-like application. Content from other applications (on other domains) is shown using iframes.

How do I resize the iframes to fit the heigh

21条回答
  •  有刺的猬
    2020-11-21 08:17

    https://developer.mozilla.org/en/DOM/window.postMessage

    window.postMessage()

    window.postMessage is a method for safely enabling cross-origin communication. Normally, scripts on different pages are only allowed to access each other if and only if the pages which executed them are at locations with the same protocol (usually both http), port number (80 being the default for http), and host (modulo document.domain being set by both pages to the same value). window.postMessage provides a controlled mechanism to circumvent this restriction in a way which is secure when properly used.

    Summary

    window.postMessage, when called, causes a MessageEvent to be dispatched at the target window when any pending script that must be executed completes (e.g. remaining event handlers if window.postMessage is called from an event handler, previously-set pending timeouts, etc.). The MessageEvent has the type message, a data property which is set to the string value of the first argument provided to window.postMessage, an origin property corresponding to the origin of the main document in the window calling window.postMessage at the time window.postMessage was called, and a source property which is the window from which window.postMessage is called. (Other standard properties of events are present with their expected values.)

    The iFrame-Resizer library uses postMessage to keep an iFrame sized to it's content, along with MutationObserver to detect changes to the content and doesn't depend on jQuery.

    https://github.com/davidjbradshaw/iframe-resizer

    jQuery: Cross-domain scripting goodness

    http://benalman.com/projects/jquery-postmessage-plugin/

    Has demo of resizing iframe window...

    http://benalman.com/code/projects/jquery-postmessage/examples/iframe/

    This article shows how to remove the dependency on jQuery... Plus has a lot of useful info and links to other solutions.

    http://www.onlineaspect.com/2010/01/15/backwards-compatible-postmessage/

    Barebones example...

    http://onlineaspect.com/uploads/postmessage/parent.html

    HTML 5 working draft on window.postMessage

    http://www.whatwg.org/specs/web-apps/current-work/multipage/comms.html#crossDocumentMessages

    John Resig on Cross-Window Messaging

    http://ejohn.org/blog/cross-window-messaging/

提交回复
热议问题