Call javascript var inside other script

时间:2015-11-12 10:54:18

标签: javascript var

I have this code:

<script type="text/javascript">
var rndn = Math.round(Math.random()*1000000);
</script>
<script type="text/javascript" src="http://myexample.com/call=cid&rnd=7676786"></script>

I need to recall the "rndn" var to replace the number 7676786 in the second script.

How do I do that?

4 个答案:

答案 0 :(得分:4)

I think this is the best way to do it:

<script type="text/javascript">
var rndn = Math.round(Math.random()*1000000);
var myScript = document.createElement('script');

myScript.setAttribute('src','http://myexample.com/call=cid&rnd='+rnd);

document.head.appendChild(myScript);
</script>

答案 1 :(得分:0)

This should work:

<script type="text/javascript" src="javascript:'http://myexample.com/call=cid&rnd='+rndn"></script>

答案 2 :(得分:0)

Simply:

<script id="other_script" type="text/javascript">

</script>

<script type="text/javascript">
var rndn = Math.round(Math.random()*1000000);

document.getElementById("other_script").setAttribute("src","http://myexample.com/call=cid&rnd="+rndn);
</script>

答案 3 :(得分:0)

I found out that the best way to do this is:

<script type="text/javascript">
var rndn = Math.round(Math.random()*1000000);
document.write('<scri'+'pt language=\"Javascript\" src=\"http://myexample.com/call=cid&rnd='+rndn+'"></scri'+'pt>');
</script>