Created
May 29, 2013 03:38
-
-
Save FL4TLiN3/5667821 to your computer and use it in GitHub Desktop.
require.jsはmainモジュールを呼び出すscriptタグ以外にdata-main属性を持つscriptタグがドキュメント中に存在している場合、死ぬ。
流石にどうにか出来ないかと思ったので、呼び出し先のjavascript内で呼び元のscriptタグを一意に参照出来ないか考えてみた。
このサンプルは同期的にDOMが評価されている場合、うまい事動く。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Find Caller Script Tag Sample</title> | |
<script src="main.js" data-main="foo.js"></script> | |
</head> | |
<body> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
var scripts = document.getElementsByTagName('script'); | |
var caller = scripts[scripts.length - 1]; | |
var name = caller.getAttribute('data-name'); | |
alert(name); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment