Skip to content

Commit 2b68227

Browse files
uberskajschulte
andauthored
Add docs on iframe resizing and best practices. (#3)
* Add docs on iframe resizing and best practices. * Force scrollbar to never display in the iframe. * Update _samples.md * Include domain in iframe src Co-Authored-By: Jonah Schulte <[email protected]> * Match origin to updated iframe src Co-Authored-By: Jonah Schulte <[email protected]> * Use cms.dealer.com domain instead Co-Authored-By: Jonah Schulte <[email protected]> * Merge Best Practices and Technical Requirements. Tweak iframe resizing documentation for clarity. Update code to be a little more generic. Co-authored-by: Jonah Schulte <[email protected]>
1 parent 4b4b21f commit 2b68227

File tree

3 files changed

+94
-5
lines changed

3 files changed

+94
-5
lines changed

source/includes/_requirements.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,18 @@ Your code should be minimal and perform only actions necessary to bootstrap your
4646
// Your code here
4747
})(window.DDC.API);
4848
```
49-
* Always ecapsulate your code in an immediately executing function expression to avoid polluting the global namespace. See sidebar for example of such a function.
49+
* **Avoid Global Scope Pollution** - Always ecapsulate your code in an immediately executing function expression to avoid polluting the global namespace. See sidebar for example of such a function.
5050

51-
* Your bootstrap script and any other integration code or assets should be located on a highly available content delivery network and must work over an HTTPS connection.
51+
* **Use a CDN** - Your bootstrap script and any other integration code or assets should be located on a highly available content delivery network and must work over an HTTPS connection.
5252

53-
* All code should be minified and served using gzip or better compression.
53+
* **Minification and Compression** - All code should be minified and served using gzip or better compression.
5454

55-
* Integrations should avoid loading excessive imagery, fonts and scripts. While some are often necessary, less is more and avoiding loading duplicate scripts will help reduce load time for your integration as well as improve overall website speed.
55+
* **Minimize Asset Size** - Integrations should avoid loading excessive imagery, fonts and scripts. While some are often necessary, less is more and avoiding loading duplicate scripts will help reduce load time for your integration as well as improve overall website speed.
5656

57-
* Please refrain from including large libraries such as jQuery or React, as both are already available on all pages of all of our sites.
57+
* **Avoid Duplicate Code** - Please refrain from including large libraries such as jQuery or React, as both are already available on all pages of all of our sites. Avoid polyfilling items that DDC already polyfills.
5858

59+
* **Browser Support** - Your code must support IE11, Edge, Firefox, Chrome, Safari, and iOS Safari.
60+
61+
* **Minimize Files** - Serve content from as few files as possible to allow for optimal downloads.
62+
63+
* **Minimize Domains** - Serve content from as few domains as possible to reduce the number of https connections the browser must make.

source/includes/_samples.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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.

source/index.html.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ includes:
1616
- utilities
1717
- jwt
1818
- debugging
19+
- samples
1920

2021
search: true
2122
---

0 commit comments

Comments
 (0)