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?

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>
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>

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>

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>

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

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.