|
| 1 | +# Sample Code |
| 2 | + |
| 3 | +The sample code here is provided as a starting point for how to accomplish tasks that are related to integrations but that may not fit squarely in the responsibility of the Integration API. Code may need to be modified to fit your integration's use case. |
| 4 | + |
| 5 | +## Resizing an `iframe` Based on Content Changes |
| 6 | + |
| 7 | +> iframe Code: |
| 8 | +
|
| 9 | +```javascript |
| 10 | +document.body.style.overflow = 'hidden'; |
| 11 | + |
| 12 | +function sendResizeMessage() { |
| 13 | + window.parent.postMessage({ |
| 14 | + type: 'IFRAME_HEIGHT_RESIZE', |
| 15 | + target: 'growing-iframe', |
| 16 | + frameHeight: document.body.offsetHeight + 10 /* a little extra for good measure */ |
| 17 | + }, '*'); |
| 18 | +} |
| 19 | + |
| 20 | +if (window.ResizeObserver) { |
| 21 | + var heightObserver = new ResizeObserver(function() { |
| 22 | + sendResizeMessage(); |
| 23 | + }); |
| 24 | + heightObserver.observe(document.body); |
| 25 | +} else { |
| 26 | + // IE 11 |
| 27 | + setInterval( |
| 28 | + sendResizeMessage, |
| 29 | + 500 /* ms */ |
| 30 | + ); |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +> Integration Code: |
| 35 | +
|
| 36 | +```javascript |
| 37 | +(function (WIAPI) { |
| 38 | + API = new WIAPI(); |
| 39 | + |
| 40 | + API.insert('content', function (elem, meta) { |
| 41 | + var iframeElem = document.createElement('iframe'); |
| 42 | + iframeElem.src = 'https://www.yourdomain.com/path-to-iframe.htm'; |
| 43 | + iframeElem.classList.add('my-integration-name-iframe'); |
| 44 | + API.append(elem, iframeElem); |
| 45 | + }); |
| 46 | + |
| 47 | + function setIframeHeight(e) { |
| 48 | + if (e.origin !== 'https://www.yourdomain.com') { |
| 49 | + // You should ALWAYS verify the origin matches the third party domain |
| 50 | + // the iframe is loaded from. For more information, see: |
| 51 | + // https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage#Security_concerns |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + if (e.data.type === 'IFRAME_HEIGHT_RESIZE' && e.data.frameHeight && e.data.target) { |
| 56 | + var iframes = document.getElementsByClassName(e.data.target); |
| 57 | + if (iframes.length === 1) { |
| 58 | + iframes[0].style.height = e.data.frameHeight + 'px'; |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + window.addEventListener('message', setIframeHeight, false); |
| 64 | +})(window.DDC.API); |
| 65 | +``` |
| 66 | + |
| 67 | +An integration may want to insert an iframe that resizes as its contents change. One possible way to accomplish this is for the iframe and the integration to work together as shown in the sample code from the pane on the right side of this page. You can see the sample code running [here](https://webapitestddc.cms.dealer.com/growing-iframe-example.htm). |
| 68 | + |
| 69 | +### iframe Responsibilities: |
| 70 | + |
| 71 | +* **Determine when content dimensions change** - One way to do this is using [`ResizeObserver`](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver). `ResizeObserver` is not supported in IE11, so one possible solution is to fallback to polling. Polling is not ideal, but it will work for this small percentage of our traffic. |
| 72 | +* **Communicate the new dimension to the outer page** - iframes can communicate with the parent page using [`postMessage`](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage). |
| 73 | +* **Ensure a scrollbar never shows up within the iframe** - Styling may be used to ensure a scrollbar never appears in the iframe. |
| 74 | + |
| 75 | +### Integration Responsibilities(on the outer page): |
| 76 | + |
| 77 | +* **Insert the iframe into the page** - You can use the API methods to insert an iframe into a location. |
| 78 | +* **Listen for resize messages and resize the iframe** |
| 79 | + |
| 80 | +### Considerations |
| 81 | + |
| 82 | +* If you use `postMessage`, ensure that you check the event's origin to alleviate [security concerns](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage#Security_concerns). |
| 83 | +* The integration resizing code supports multiple iframes from the same vendor. You may need to modify the code to target your iframes differently. |
0 commit comments