Javascript 使用 JS 变量设置 <script> 标签的 src 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11150409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:41:19  来源:igfitidea点击:

Use JS variable to set the src attribute for <script> tag

javascriptjspjsp-tags

提问by 1985percy

I want to use a javascript variable as a 'src' attribute for another tag on the same jsp.

我想使用一个 javascript 变量作为同一 jsp 上另一个标签的“src”属性。

<script>
var link = mylink // the link is generated based on some code
</script>

I want to create this new element as shown below.

我想创建这个新元素,如下所示。

<script src="mylink">
</script>

On searching various forums, I have tried using the following options but they don't seem to work. I want this thing to work on all major browsers.

在搜索各种论坛时,我尝试使用以下选项,但它们似乎不起作用。我希望这个东西可以在所有主要浏览器上运行。

  1. Put this code in the first element.

    var script   = document.createElement("script");
    script.type  = "text/javascript";
    script.src   = "path/to/somelink";
    document.body.appendChild(script);
    
  2. Use document write method in the first element.

    document.write("<script type='text/javascript' src="+ google.com + "><\/script>");
    
  3. Tried to set a JSTL Variable in the first element and use it.

    <c:set var="URL" value="mylink"/>
    
  1. 将此代码放在第一个元素中。

    var script   = document.createElement("script");
    script.type  = "text/javascript";
    script.src   = "path/to/somelink";
    document.body.appendChild(script);
    
  2. 在第一个元素中使用文档写入方法。

    document.write("<script type='text/javascript' src="+ google.com + "><\/script>");
    
  3. 试图在第一个元素中设置一个 JSTL 变量并使用它。

    <c:set var="URL" value="mylink"/>
    

None of these ways were successful. Any suggestions on what is going wrong?

这些方法都没有成功。关于出了什么问题的任何建议?

回答by Mahesh

Though CDATA works fine, using document.createElement is also a great choice.. Especially if you intend to append some value to a URL, say for cache busting..

尽管 CDATA 工作正常,但使用 document.createElement 也是一个不错的选择..特别是如果您打算向 URL 附加一些值,例如缓存破坏..

<script type="text/javascript"> 
    var JSLink = "/Folder/sub_folder/version.js?version=" + Math.random();
    var JSElement = document.createElement('script');
    JSElement.src = JSLink;
    JSElement.onload = OnceLoaded;
    document.getElementsByTagName('head')[0].appendChild(JSElement);

    function OnceLoaded() {
        // Once loaded.. load other JS or CSS or call objects of version.js
    }
</script>

Code well.. :)

代码很好.. :)

回答by Gavy

I use something similar to choice two. There is a slight mistake in your code because "google.com" needs to be surrounded by quotes.

我使用类似于选择二的东西。您的代码中有一个小错误,因为“google.com”需要用引号括起来。

To improve compatibility, you might want to write it as:

为了提高兼容性,您可能希望将其编写为:

document.write("<script type='text/javascript' src='"+ x + "'><\/scr" + "ipt>");

In this situation, xwould be the file to be included. You can define it as:

在这种情况下,x将是要包含的文件。您可以将其定义为:

var x = "http://google.com/script.js";

OR

或者

var x = "path/to/script.js";

回答by JonWarnerNet

Are you able to use jQuery? If so you could use getScript():

你会使用 jQuery 吗?如果是这样,您可以使用getScript()

http://api.jquery.com/jQuery.getScript/

http://api.jquery.com/jQuery.getScript/

$.getScript(mylink, function() {
   // do something using the JS that was loaded.
});

回答by chaoskreator

Try:

尝试:

(function(d){
     var file = 'yourJS.js';
     var ref = d.getElementsByTagName('script')[0];
     var js = d.createElement('script');
     js.src = file;
     ref.parentNode.insertBefore(js, ref);
}(document));

What this does:

这是做什么的:

  1. Find the first script element on your page
  2. Creates a new script element with your supplied source.
  3. Then inserts that new element before the first existing script element.
  1. 找到页面上的第一个脚本元素
  2. 使用您提供的源创建一个新的脚本元素。
  3. 然后在第一个现有脚本元素之前插入该新元素。

回答by Zon

<xsl:variable name="Path" select="/root/folder/"></xsl:variable> <!-- Global path variable. -->
<xsl:variable name="myScriptPath" select="concat($Path, 'myScript.js')"></xsl:variable> <!-- Relative script path variable. -->
<script src="{$myScriptPath}"/> <!-- Attach script. -->