0

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?

Tim B
  • 40,716
  • 16
  • 83
  • 128
Nicolò Canal
  • 11
  • 2
  • 5

4 Answers4

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>
Pradeep
  • 3,093
  • 17
  • 21
  • I like it, but why not simply add a new 'src' attribute to an existing script node? Check my answer to understand what I am saying – Luis González Nov 12 '15 at 11:06
  • @Pradeep I would go for this method **if** the script tag only needed to be created once but if the OP want's to change the url parameter for whatever reason I would give the script tag an `id`, check if the element exists, if so update it, if not then create it. – NewToJS Nov 12 '15 at 11:10
  • @NewToJS this is just a demo, OP can customize it to his/her requirement. – Pradeep Nov 12 '15 at 11:23
0

This should work:

<script type="text/javascript" src="javascript:'http://myexample.com/call=cid&rnd='+rndn"></script>
kasynych
  • 71
  • 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>
Luis González
  • 3,199
  • 26
  • 43
  • 1
    `document.getElementById("other_script")` will be `null`, and why the jQuery part? – Andreas Nov 12 '15 at 11:05
  • 1
    This should be placed in - `window.onload=function(){//Here}` otherwise the javascript will run before the browser knows `other_script` exists or it could be placed in another function and have `window.onload=MyFunc;` to call it when the DOM is ready. – NewToJS Nov 12 '15 at 11:07
  • @NewToJS Not necessarily. Just swap the script blocks. – Andreas Nov 12 '15 at 11:09
  • I understood, so I changed the order. – Luis González Nov 12 '15 at 11:09
  • 1
    @Andreas You can just swap the script tags but the point is the original answer wouldn't work. I was offering a way of fixing the answer. – NewToJS Nov 12 '15 at 11:11
  • @NewToJS The browser "knows" the element as soon as the parser hits it in the markup – Andreas Nov 12 '15 at 11:17
  • 1
    @Andreas So why swap the order of the script tags? Now you're contradicting yourself. The original answer luigonsec posted would return `Uncaught TypeError: Cannot read property 'setAttribute' of null` in the console hence me offering a solution to fix the answer. The browser has to read the elements before you can attempt to change them. – NewToJS Nov 12 '15 at 11:22
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>
Tim B
  • 40,716
  • 16
  • 83
  • 128
Nicolò Canal
  • 11
  • 2
  • 5
  • @NewToJS There's absolutely no problem with `document.write()` as long as it is used before the page has finished loading. – Andreas Nov 12 '15 at 11:19