[英]Can't append script element to head
Current variant looks like that (I tried solution offered here: Can't append <script> element ): 当前变体如下所示(我尝试了此处提供的解决方案: 无法追加<script> element ):
var s=document.createElement("script");
s.type="text/javascript";
s.src="js/properties.js";
$("head").append(s);
Previous variant was: 先前的变体是:
$("head").append($('<script type="text/javascript" src="js/properties.js"></script>'));
And both of them don't work. 而且它们都不起作用。 "properties.js" is also in "js" folder, but if I remove this part of path, it doesn't change anything.
“ properties.js”也位于“ js”文件夹中,但是如果删除路径的这一部分,则不会更改任何内容。 I also tried to use ' instead " and check addBlock: I had it installed, but it's disabled on this page. Changing "append" function to "appendChild" also didn't help.
我也尝试使用'相反的'并检查addBlock:我已经安装了它,但是在此页面上将其禁用。将“ append”函数更改为“ appendChild”也无济于事。
"properties.js" contains just one line: “ properties.js”仅包含一行:
var PREFIX_URL = "http://localhost:8080/app-rest-1.0.0-SNAPSHOT";
And firstly I declare it in "main.js" to which I, in fact, try to connect this file. 首先,我在“ main.js”中声明它,实际上,我尝试将其连接到该文件。 Explain, please, what I'm doing wrong.
请解释一下我在做什么错。
Add all your <script>
tags right before the closing </body>
tag, because when the browser encounters a <script>
tag it begins downloading it and stops rendering of the page. 在结束
</body>
标记之前添加所有<script>
标记,因为当浏览器遇到<script>
标记时,它将开始下载它并停止呈现页面。 So by placing them at the bottom of the page you make sure your page is fully loaded before trying to interact with the DOM elements. 因此,通过将它们放置在页面底部,可以确保在尝试与DOM元素进行交互之前页面已完全加载。 Also
$("head")
returns an array of all the <head>
tags. $("head")
还会返回所有<head>
标签的数组。 You should also enclose your calls in a $(document).ready()
function. 您还应该将调用包含在
$(document).ready()
函数中。
<!-- Your html tags here -->
<script type="text/javascript">
$(document).ready(function(){
var s=document.createElement("script");
s.type="text/javascript";
s.src="js/properties.js";
$("head")[0].append(s);
});
</script>
</body>
I made JSBin example . 我以JSBin 为例 。 You can see in console that last script tag is the one you need.
您可以在控制台中看到所需的最后一个脚本标记。 So the your code is correct.
因此,您的代码是正确的。
If your IDE don't highlight 'var' - it may be error not in javascript. 如果您的IDE不突出显示“ var”-可能不是JavaScript的错误。 You can place it in a wrong place for example.
例如,您可以将其放置在错误的位置。
Can you provide link to a gist (or pastie.org or smth) for us to better understand your problem. 您能否提供要点(或pastie.org或smth)的链接,以便我们更好地了解您的问题。
PS The code $("head")[0].append gives me undefined ( note to previous answer) PS代码$(“ head”)[0] .append给我未定义(注意上一个答案)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.