开发者

Loading files through goog.require in a script block fails

开发者 https://www.devze.com 2022-12-11 23:17 出处:网络
When using goog.require inside a <script> tag to load a script, the specified files aren\'t loaded. For example:

When using goog.require inside a <script> tag to load a script, the specified files aren't loaded. For example:

<script>
goog.require('goog.dom');
var mydiv = goog.dom.$('foo');
</script>

Gives:

goog.dom is undefined

What's开发者_如何学编程 wrong with this usage?


The problem is that goog.require dynamically adds the required script tags to the document after the current script tag. So, for example:

<script>
goog.require('stuff');

doSomething();
</script>

Translates to:

<script>
goog.require('stuff');

doSomething();
</script>
<script src=[included stuff] type="text/javascript"></script>

The solution is to have a separate script tag for requirements:

<script>
goog.require('stuff');
</script>

<script>
doSomething();
</script>
0

精彩评论

暂无评论...
验证码 换一张
取 消