Skip to content

Instantly share code, notes, and snippets.

@aloerina01
Last active October 14, 2016 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aloerina01/51e8012367ec04c78c37128d0430b428 to your computer and use it in GitHub Desktop.
Save aloerina01/51e8012367ec04c78c37128d0430b428 to your computer and use it in GitHub Desktop.
document.writeを含むライブラリの動的読込+同期実行
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script>
(function() {
if (!conditions) { // 条件は省略
return;
}
// 1. create iframe to load a library
var iframe = document.createElement('iframe');
var head = document.getElementsByTagName('head')[0];
head.appendChild(iframe);
// 2. some functions after loading a library
iframe.onload = function() {
// 後続処理は省略
}
// 3. write sctipt tag in iframe
var html = '<body><script src="//scdn.hogehoge.com/library.js"><\/script><\/body>';
var iframeDocument = iframe.contentWindow.document;
iframeDocument.open();
iframeDocument.write(html);
iframeDocument.close();
})();
</script>
</head>
<body>
<!-- 省略 -->
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment