简体   繁体   English

在Javascript中传递变量 - 不执行

[英]Passing Variables in Javascript - Doesnt Execute

I have written following code to pass File1 variable to Javascript, but its not executing, and I'm not sure why. 我编写了以下代码将File1变量传递给Javascript,但它没有执行,我不知道为什么。 When I use alert with File1, it works - but the document.write script is not working. 当我使用File1的警报时,它可以工作 - 但document.write脚本不起作用。 Any help? 有帮助吗?

<script type="text/javascript">
var Order[0]="1";
var Order[2]="2";
var Order[3]="4";
var File1=Order[2]+"/"+Order[0]+"/"+%Order[4];

document.write("<script type='javascript' src=http://abc.com/i_sale_third/10957/'" + File1 + ">";

</script>

Well, you did mess up your single quotes in the document.write . 好吧,你确实搞乱了document.write单引号。 I fixed it in this, see if it works: 我把它修好了,看它是否有效:

<script type="text/javascript">
var Order[0]="1";
var Order[2]="2";
var Order[3]="4";
var File1=Order[2]+"/"+Order[0]+"/"+%Order[4];

document.write("<script type='javascript' src='http://abc.com/i_sale_third/10957/" + File1 + "'>";

</script>

Edits. 编辑。 Try the following code. 请尝试以下代码。

Maybe the little % is messing it up too right before Order[4] (and beside the fact that you may not have defined Order[4] ). 也许在Order[4]之前,小%正在弄乱它(除了你可能没有定义Order[4]的事实)。 I also added the console.log to your code so open up your console (in Chrome and Safari it is dev tools). 我还将console.log添加到您的代码中,以便打开您的控制台(在Chrome和Safari中它是开发工具)。 You also didn't need to repeat var keyword (you can separate them by commas if you didn't know) and according to @ajax333221 (and me) you need to initialize Order by doing Order = [] : 您也不需要重复var关键字(如果您不知道,可以用逗号分隔它们),根据@ ajax333221(和我),您需要通过执行Order = []来初始化Order

<script type="text/javascript">
var Order = [],
    Order[0] = "1",
    Order[2] = "2",
    Order[3] = "4",
    File1 = Order[2] + "/" + Order[0] + "/" + Order[3]; // I think you meant Order[3] not Order[4] here

if(console) console.log(File1); // this will print File1 into the console so you can see the string output

document.write("<script type='javascript' src='http://abc.com/i_sale_third/10957/" + File1 + "'></script>");
</script>

try with this: 试试这个:

<script type="text/javascript">
    var Order = []; //created the array
    Order[0] = "1";
    Order[2] = "2";
    Order[3] = "4";

    var File1 = Order[2] + "/" + Order[0] + "/" + Order[3]; //added var and changed Order[3]

    document.write("<script type='javascript' src='http://abc.com/i_sale_third/10957/" + File1 + "'></script>"); //fixed quotes placement and closed with </script>
</script>

Useful links to read: 有用的链接:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM