Downloading files using goog.require in a script block is not performed

When used goog.requireinside a tag <script>to load a script, the specified files are not loaded. For example:

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

gives:

goog.dom is undefined

What is wrong with this use?

+3
source share
1 answer

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

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

doSomething();
</script>

Translated to:

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

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

The solution must have a separate script tag for requirements:

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

<script>
doSomething();
</script>
+9
source

Source: https://habr.com/ru/post/1722826/


All Articles