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
41.3k16 gold badges88 silver badges132 bronze badges
asked Nov 12, 2015 at 10:54
Nicolò Canal
112 silver badges5 bronze badges
-
You can find answer here: stackoverflow.com/questions/950087/…Alexander Elgin– Alexander Elgin2015年11月12日 11:00:58 +00:00Commented Nov 12, 2015 at 11:00
4 Answers 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>
Sign up to request clarification or add additional context in comments.
3 Comments
Luis González
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
NewToJS
@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.Pradeep
@NewToJS this is just a demo, OP can customize it to his/her requirement.
This should work:
<script type="text/javascript" src="javascript:'http://myexample.com/call=cid&rnd='+rndn"></script>
Comments
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>
answered Nov 12, 2015 at 11:03
Luis González
3,6691 gold badge29 silver badges49 bronze badges
7 Comments
Andreas
document.getElementById("other_script") will be null, and why the jQuery part?NewToJS
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.Andreas
@NewToJS Not necessarily. Just swap the script blocks.
NewToJS
@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
@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. |
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
41.3k16 gold badges88 silver badges132 bronze badges
answered Nov 12, 2015 at 11:11
Nicolò Canal
112 silver badges5 bronze badges
1 Comment
Andreas
@NewToJS There's absolutely no problem with
document.write() as long as it is used before the page has finished loading.lang-js