ポップアップ風のウィンドウをhtmlのiframeを用いて実装しました。
プレビュー機能の実装をしています。
URLは都合上見せることはできません。申し訳ございません。
前提としては、同じサーバー内に
- sample.com
- sample2.com
というドメインがあります。
sample.comでプレビュー画面を押下すると、
sample2.comのpreview.htmlがiframeで呼ばれてポップアップのように表示されます。
しかし、google chromeのデベロッパーツールでみてみると以下のエラーが表示され
CSSを読み込むことができません。
txt
1Uncaught DOMException: Blocked a frame with origin "http://sample.com" from accessing a cross-origin frame. 2 at contents (https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js:5:23123) 3 at Function.map (https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js:4:6171) 4 at init.x.fn.<computed> [as contents] (https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js:5:23203) 5 at HTMLIFrameElement.<anonymous> (http://sample.com:28:24) 6 at HTMLIFrameElement.dispatch (https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js:5:14113) 7 at HTMLIFrameElement.v.handle (https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js:5:10850)
以下は、jQueryのコードです。
直接htmlに埋め込んでいます。
html
1sample.com/contents/main.html 2 <script> 3 4 //previewのiframeを追加 5 $(function(){ 6 $('#pre_iframe').append('<iframe src="http://sample2.com/contents/views/html/preview.html></iframe>'); 7 }); 8 9 //CSSをiframeに使用できるようにする 10 $(function(){ 11 $('#preview').on('load', function(){ 12 $('#preview').contents().find('head').append('<link rel="stylesheet" href="http://sample2.com/contents/.css">'); 13 $('body').css('overflow','hidden'); 14 }); 15 }); 16 17 //クローズイベント 18 $(document).on('click', '#close', function(){ 19 $('#preIframe').remove(); 20 $('body').css('overflow','scroll'); 21 }); 22 23 </script>
safariでは、「開発 > クロスオリジンの制限を無効にする」にチェックを入れれば綺麗に表示されます。
この場合、どのようにしてクロスドメイン制限を解除したら良いでしょうか。
JS上で解決できれば、それに越したことはないのですが、他に簡単な方法はないでしょうか。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/06/19 01:58
退会済みユーザー
2019/06/19 01:59
退会済みユーザー
2019/06/19 02:05 編集