I have the following script:
<script id="lol" type="text/javascript" src="http://pn.lol.de/get_trans.cgi?cpid=1&tid=999999&produkt=7&umsatz=0&kunde=raffle_201406&js=1"></script>
I want the tid of the ´src´attribute to be a random number and not 999999, so I wrote a script before it which tries to change it:
<script>
var transactionId = Math.floor(Math.random() * 100000); //Generate pseudorandom id
var element = document.getElementById("lol");
var newsrc = "http://pn.lol.de/get_trans.cgi?cpid=1&tid=" + transactionId + "&produkt=7&umsatz=0&kunde=raffle_201406&js=1";
element.setAttribute("src", newsrc);
</script>
But that does not seem to work. Am I doing something wrong? Is this a better way to achieve this?
UPDATE
What I want to have is the following code but with random tid parameter on the src:
<script type="text/javascript" src="http://pn.lol.de/get_trans.cgi?cpid=1&tid=t26673&produkt=Raffle201406&umsatz=1&kunde=raffle_201406&js=1"></script>
<noscript><img src="http://pn.lol.de/get_trans.cgi?cpid=1&tid=t26673&produkt=Raffle201406&umsatz=1&kunde=raffle_201406" height="3" width="2" border="0" /></noscript>
So I wrote the following:
<script>
var transactionId = Math.floor(Math.random() * 100000); //Generate pseudorandom id
var element = document.createElement('script');//Create new script element
var newsrc = "http://pn.lol.de/get_trans.cgi?cpid=1&tid=" + transactionId + "&produkt=Raffle201406&umsatz=0&kunde=raffle_201406&js=1";
element.setAttribute("src", newsrc);
element.setAttribute("type","text/javascript");
var elementNoscript = document.createElement('noscript');//Create new script element
var elementImg = document.createElement('img');
elementImg.setAttribute("src",newsrc);
elementImg.setAttribute("height",3);
elementImg.setAttribute("width",2);
elementImg.setAttribute("border",0);
document.body.appendChild( element ); // Insert it into document
elementNoscript.appendChild(elementImg);
document.body.appendChild( elementNoscript );
</script>
But that doesn't seem to work, any idea where is my mistake?
#loldoesn't exist at the time you try to use it, (even if it would, you can't change the script once it is loaded.) This is not possible. You have to create the attribute on server-side, or load the script dynamically.head,bodydoesn't exist at the time you try to append a new element. Try to append todocument.getElementsByTagName('head')[0]instead ofbody.noscriptmust be appended tobody. You've to separate the task into two parts or move all the script to thebody.