ã¯ããã«
ãã«ãªãã®ç¸£ã§ããä¸çªå¥½ããªJavaScriptã®é¢æ°ã¯postMessageã§ãï¼
ã¡ããåã«iframeã§ã®åãè¾¼ã¿ã§ã¹ã¯ãã¼ã«ãã¼ãåºããã«ããã¿ãªã®é«ãã«ä¸èº«ã¨iframeã®é«ããåæãå¿ è¦ãªè¦ä»¶ãããã¾ããã ãã®éã«ResizeObserverã¨postMessageã使ã£ã¦ãã¾ãå®è£ ãã§ããã®ã§ããã®æ¹æ³ããç´¹ä»ãã¾ãã
iframeã®é«ã調æ´åé¡ã¨ã¯ï¼
ããã§ã¯ä¾¿å®çã«ä»¥ä¸ã®ããã«å¼ã³ã¾ãã
- 親ãã¼ã¸(https://parent.example.com/parent.html)ï¼iframeã使ã£ã¦ä»ã®ãã¼ã¸ãåãè¾¼ãå´ã®ãã¼ã¸
- åãã¼ã¸(https://child.example.com/child.html)ï¼iframeå ã§è¡¨ç¤ºããããã¼ã¸
ä¸è¨ã®å ´åãåãã¼ã¸ã®é«ããå¯å¤ã®å ´åãiframeã®é«ãã決ãæã¡ã§è¨å®ãã¦ããã¨ç¸¦æ¹åã®ã¹ã¯ãã¼ã«ãã¼ã表示ããã¦ãã¾ãã¾ãã
親ãã¼ã¸å´ã§åãã¼ã¸ã®ã³ã³ãã³ãã®é«ããåããã°ããã®ã§ãããã¯ãã¹ãã¡ã¤ã³ã®å¶ç´ã«ããiframeå ã®è¦ç´ ã«ã¯è¦ªãã¬ã¼ã ããã¯ã¢ã¯ã»ã¹ãã§ãã¾ããã
ãã¹ã¯ãããã¨ã¢ãã¤ã«ã§ä½è£ãæã£ãé«ãããªãã¨ãªãiframeã«è¨å®ããã¨ããã®ãããããåé¿çã§ã¯ãªããã¨æãã¾ãã
ãã®åé¡ã¯ResizeObserverã¨postMessageã使ãã¨ããæãã«ã¯ãªã¢ã§ãã¾ãã以ä¸ããã®æ¹æ³ã§ãã
1. åãã¼ã¸ã®é«ããå¤ãã£ããã¨ãåãã¼ã¸å ã§æ¤ç¥ãã
以ä¸ã®ããã«ResizeObserverã使ããã¨ã§ç°¡åã«ãµã¤ãºå¤æ´ãæ¤ç¥ã§ãã¾ãã*1
åãã¼ã¸
const resizeHandler = () => { console.log('ç¾å¨ã®é«ã: ' + document.documentElement.offsetHeight); }; const setup = () => { // iframeå ã«ãããã©ããã®ãã§ã㯠if (window.parent === window) { // iframeå ã§ã¯ãªãå ´åã¯ä½ãããªã return; } const resizeObserver = new ResizeObserver(resizeHandler); // ããã¥ã¡ã³ãã®ãµã¤ãºå¤æ´ãç£è¦ resizeObserver.observe(document.documentElement); }; setup();
2. åãã¼ã¸ã®é«ããå¤ãã£ãã親ãã¼ã¸ã«ã¡ãã»ã¼ã¸ãéã
ResizeObserverã§æ¤ç¥ããé«ãã®å¤æ´ãpostMessageã使ã£ã¦è¦ªãã¼ã¸ã«ã¡ãã»ã¼ã¸éä¿¡ãã¾ããå
·ä½çã«ã¯window.parent.postMessage
ã§è¦ªãã¼ã¸ã«ã¡ãã»ã¼ã¸ãéããã¨ãã§ãã¾ãã
postMessageã§éãã¡ãã»ã¼ã¸ã¯ä»»æã®å½¢å¼ã使ãã¾ããããã§ã¯ã¡ãã»ã¼ã¸ã®ç¨®å¥ãtype
ãç¾å¨ã®é«ããheight
ã¨ãã¦éã£ã¦ãã¾ãã
åãã¼ã¸
const resizeHandler = () => { // 親ãã¼ã¸ã«ã¡ãã»ã¼ã¸ãéä¿¡ window.parent.postMessage( { type: 'document-height-changed', height: document.documentElement.offsetHeight, }, 'https://parent.example.com' ); }; export const setupWidgetMode = () => { // iframeå ã«ãããã©ããã®ãã§ã㯠if (window.parent === window) { // iframeå ã§ã¯ãªãå ´åã¯ä½ãããªã return; } const resizeObserver = new ResizeObserver(resizeHandler); // ããã¥ã¡ã³ãã®ãµã¤ãºå¤æ´ãç£è¦ resizeObserver.observe(document.documentElement); }; setup();
3. 親ãã¼ã¸ã§ã¡ãã»ã¼ã¸ãåãåãiframeã®é«ããå¤æ´ãã
window.addEventListener('message',
ã§postMessageã§éããã¦ããã¡ãã»ã¼ã¸ãåä¿¡ãã¦ãé«ãã®å¤æ´ãä¼ããã¡ãã»ã¼ã¸ã§ããã°iframeã®é«ããå¤æ´ãã¾ãã
ããã«ãããåãã¼ã¸ã®é«ãã¨iframeã®é«ãã常ã«åæããããã«ãªãã¾ãã
親ãã¼ã¸
<iframe id="iframe" src="https://child.example.com/child.html"></iframe> <script type="text/javascript"> window.addEventListener('message', (event) => { // ã¡ãã»ã¼ã¸ã®éä¿¡å ãªãªã¸ã³ããã§ã㯠if (event.origin !== 'https://child.example.com') { return; } if (event.data.type === 'document-height-changed') { if (event.data.height) { // ææ°ã®é«ããiframeã«è¨å® document.querySelector('#iframe').style.height = event.data.height + 'px'; } } }) </script>
ã¾ã¨ã
- è¦ç´ ã®ãµã¤ãºå¤æ´ãæ¤ç¥ããã«ã¯ReizeObserverã便å©
- ã¯ãã¹ãã¡ã¤ã³éã§ã®ã¡ãã»ã¼ã¸ã®ããã¨ãã¯postMessageã便å©
- ãã®ï¼ã¤ãçµã¿åãããã°iframeã®é«ã調æ´åé¡ã解決ã§ãã
ãããã«
ãã«ãªãã§ã¯ãã®ããã«Webæè¡ãé©æé©æã§æ´»ç¨ããµã¼ãã¹ãéçºãã¦ãã¾ãã ãã«ãªãã®ã¨ã³ã¸ãã¢ãªã³ã°ã«èå³ãããæ¹ã¯ããã²ä»¥ä¸ãã覧ãã ããã
*1:æãResizeObserverããªãæ代ã¯300msãã¨ã«ãµã¤ãºããã§ãã¯ãããªã©ã®å¯¾å¿ãå¿ è¦ã ã£ãã®ã§ã大å¤ããæ代ã«ãªãã¾ããã