我如何在脚本标签的src中使用变量? [英] How can I use a variable in the src of a script tag?

查看:2944
本文介绍了我如何在脚本标签的src中使用变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用变量 sym 来改变脚本在页面上的加载方式。如果我硬编码src URL,我可以加载脚本,但我无法使用JavaScript在脚本标记的 src 属性中构建URL。

< script>
var sym =MSFT;
document.write(sym);
< / script>

< script src =http://markets.financialcontent.com/stocks?Module=snapshot&Ticker=$SPX+MSFT&Output=JS>< / script>
<! - works - >

< script src =http://markets.financialcontent.com/stocks?Module=snapshot&Ticker=$SPX++ sym +& Output = JS>< / script> ;
<!---不起作用,因为我不知道如何正确插入var - >


解决方案


一个变量作为URL的一部分?


简单!既然你不介意使用javascript,一个简单的解决方案如下所示:

  var scriptElement = document.createElement(script); 
scriptElement.src =http://markets.financialcontent.com/stocks?Module=snapshot&Ticker=+ encodeURIComponent($ SPX ++ sym)+& Output = JS;
document.head.appendChild(scriptElement);

此代码创建一个脚本元素,其源代码为

最终的HTML为:

 <脚本> 
var sym =MSFT;
document.write(\< script src ='+http://markets.financialcontent.com/stocks?Module=snapshot&Ticker=+ encodeURIComponent($ SPX ++ sym)+ & Output = JS+'\> \< / script \>);
< / script>

这个解决方案不起作用的原因如下:

Codepen是一个HTTPS网站,这意味着它不会提供HTTP内容。说实话,我不知道为什么Codepen服务于HTTP脚本。如何解决这个问题?那么,真的只有一个简单的解决方案:

不使用Codepen,而是使用本地HTML文件并打开该文件,如果您拥有自己的HTTP服务器,则可以使用该文件。



快速操作指南:
$ b $ 1打开文本编辑器

2)输入以下内容:

 < html> 
< head>
< script>
var sym =MSFT;
document.write(\< script src ='+http://markets.financialcontent.com/stocks?Module=snapshot&Ticker=+ encodeURIComponent($ SPX ++ sym)+ & Output = JS+'\> \< / script \>);
< / script>
< / head>
< body>
< / body>
< / html>

3)现在点击另存为按钮并保存为.html文件



4)打开它!



<5>玩得开心:)

I want to use a variable sym to change how a script loads on the page. I can load the script if I hard code the src URL, but I can't use JavaScript to build the URL within the src attribute of the script tag.

<script>
    var sym = "MSFT";
    document.write(sym);
</script>

<script src="http://markets.financialcontent.com/stocks?Module=snapshot&Ticker=$SPX+MSFT&Output=JS"></script>
<!-- works -->

<script src="http://markets.financialcontent.com/stocks?Module=snapshot&Ticker=$SPX+"+sym+"&Output=JS"></script>
<!--- does NOT work because I do not know how to insert the var correctly -->

解决方案

How do you pass a variable as a part of an URL?

Easy! Since you don't mind using javascript a simple solution is as follows

var scriptElement = document.createElement("script");
scriptElement.src = "http://markets.financialcontent.com/stocks?Module=snapshot&Ticker=" + encodeURIComponent("$SPX+" + sym) + "&Output=JS";
document.head.appendChild(scriptElement);

This code creates an script element with the source being "http://markets.financialcontent.com/stocks?Module=snapshot&Ticker=" + encodeURIComponent("$SPX+" + sym) + "&Output=JS". Then it appends it to the head of the webpage.

The final HTML being:

<script>
    var sym = "MSFT";
    document.write("\<script src='" + "http://markets.financialcontent.com/stocks?Module=snapshot&Ticker=" + encodeURIComponent("$SPX+" + sym) + "&Output=JS" + "'\>\</script\>");        
</script>

The reason that this solution won't work is as follows:

Codepen is an HTTPS site which means it WILL NOT serve HTTP content. To be honest I have no idea why Codepen serves the HTTP script. How to fix this? Well, there really is only one easy solution:

Instead of using Codepen use a local HTML file and just open that, if you have your own HTTP server you can use that.

Quick how-to guide:

1) Open a text editor

2) Type the following

<html>
    <head>
        <script>
            var sym = "MSFT";
            document.write("\<script src='" + "http://markets.financialcontent.com/stocks?Module=snapshot&Ticker=" + encodeURIComponent("$SPX+" + sym) + "&Output=JS" + "'\>\</script\>");
        </script>
    </head>
    <body>
    </body>
</html>

3) Now hit the "Save As" button and save it as a .html file

4) Open it!

5) Have fun :)

这篇关于我如何在脚本标签的src中使用变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆