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
41.3k16 gold badges88 silver badges132 bronze badges
asked Nov 12, 2015 at 10:54
1

4 Answers 4

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>
answered Nov 12, 2015 at 10:58
Sign up to request clarification or add additional context in comments.

3 Comments

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
@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 this is just a demo, OP can customize it to his/her requirement.
0

This should work:

<script type="text/javascript" src="javascript:'http://myexample.com/call=cid&rnd='+rndn"></script>
answered Nov 12, 2015 at 11:01

Comments

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>
answered Nov 12, 2015 at 11:03

7 Comments

document.getElementById("other_script") will be null, and why the jQuery part?
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 Not necessarily. Just swap the script blocks.
@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.
@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.
|
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
41.3k16 gold badges88 silver badges132 bronze badges
answered Nov 12, 2015 at 11:11

1 Comment

@NewToJS There's absolutely no problem with document.write() as long as it is used before the page has finished loading.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.