Skip to content

Instantly share code, notes, and snippets.

@kfly8
Last active December 21, 2015 16:29
Show Gist options
  • Select an option

  • Save kfly8/6333777 to your computer and use it in GitHub Desktop.

Select an option

Save kfly8/6333777 to your computer and use it in GitHub Desktop.
ajaxでscript タグの入ったHTMLを読み込んだときに、scriptを読み込んでくれないのはどうして?
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8" />
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
</head>
<body>
hello!!
<div id="hoge"></div>
<script>
$(document).ready(function() {
$.get("./script.html", function(data) {
$("#hoge").html(data);
});
});
</script>
</body>
</html>
script load..
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.1/underscore-min.js"></script>
<script>
console.log(_); // defined? No!! not defined...
</script>
script load..
<script>
document.open();
document.write("<script src='//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.1/underscore-min.js'><\/script>");
document.close();
console.log(_); // _ is defined.. but script is written in head tag and "script loaded" and "hello!!" is deleted.
</script>
@kfly8

kfly8 commented Aug 25, 2013

Copy link
Copy Markdown
Author

script2.htmlで試した場合の挙動と比較すると、
headタグ内に書かないときちんとブロックして読み込んでくれない?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment