0

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?

5
  • Are you sure a tid of the value you generate is valid according to pn.lol.de 's API ?? Commented Jun 26, 2014 at 11:37
  • #lol doesn'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. Commented Jun 26, 2014 at 11:38
  • @paradoxyes tid is correct, I am sure about that. I was thinking if it is a better way to add something random in the script without using another script Commented Jun 26, 2014 at 11:39
  • You can't reload scripts like that. You have to make a new element with new src then append to body. Commented Jun 26, 2014 at 11:50
  • @Avraam If this script is within head, body doesn't exist at the time you try to append a new element. Try to append to document.getElementsByTagName('head')[0] instead of body. noscript must be appended to body. You've to separate the task into two parts or move all the script to the body. Commented Jun 26, 2014 at 14:52

2 Answers 2

3

I would dynamicaly insert script element instead of changing src property of already existing one. This should work for sure:

<script>

   var transactionId = Math.floor(Math.random()  * 100000); //Generate pseudorandom id

   var element = document.createElement('script');//Create new script element
   var newsrc = "http://pn.discavo.de/get_trans.cgi?cpid=1&tid=" + transactionId + "&produkt=7&umsatz=0&kunde=raffle_201406&js=1";
       element.setAttribute("src", newsrc);
   document.body.appendChild( element ); // Insert it into document


</script>
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, hurried a little there.
@Avraam Notice, that dynamically added scripts are loaded asynchronously.
@Teemu can be this the reason that my script is not seem to run?
0

Try Like

document.body.appendChild(document.createElement('script')).src='New JS Likns';

Comments

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.