-1

This is my JavaScript:

<script type="text/javascript">
    (function () {
        var lidd = 1;
        var ga1 = document.createElement('script'); ga1.type = 'text/javascript';
        ga1.async = true;           
        var res = ["A1LpRTPOUKU%3d", "a8g%2bUPW0%2bck%3d", "ptb0PT3OMIc%3d", "8NzewxsG6Zc%3d"];
        for (var i = 0; i < res.length; i++) {
            alert('mid=' + res[i]);
            ga1.src = ('https:' == document.location.protocol ? 'https://ssl' :
            'http://') + 'localhost:59115/ui/trackconversion.aspx?cid=CAR9nmKJG1A%3d&mid=' + res[i] + '&lid=' + lidd;
            var s1 = document.getElementsByTagName('script')[0];
            s1.parentNode.insertBefore(ga1, s1);
        }
    })();                  
</script>

My issue is that the for loop is not working properly in this code. Here the length of res is 4, hence the loop should be executed 4 times, but in my code it's only executed partially. This means, when I execute the code, I get alert('mid=' + res[i]); 4 times showing all the four values, i.e., "A1LpRTPOUKU%3d", "a8g%2bUPW0%2bck%3d", "ptb0PT3OMIc%3d", and "8NzewxsG6Zc%3d". But after that, my trackconversion.aspx is hit only once instead of getting hit 4 times.

What am I doing wrong?

7
  • 3
    Side note, instead of alert, you can use console.log and inspect with debug tools. Apr 30, 2015 at 10:09
  • Please read How to Ask for information on how to write a good question. The way it's written now, your question title is extremely uninformative.
    – dandan78
    Apr 30, 2015 at 10:58
  • 3
    Meta discussion about this question's title: meta.stackoverflow.com/questions/291905/…
    – user000001
    Apr 30, 2015 at 11:03
  • @Joe: Meaningful but inaccurate is not really an improvement
    – Ben Voigt
    Apr 30, 2015 at 18:39
  • @BenVoigt I don't fully understand the question (I try to avoid JavaScript) so I took what was posted in the question and extrapolated something out of it. It was certainly an improvement on "Error in my JavaScript" as it at least highlights the OP was expecting something to happen x times, but it only happened y.
    – Joe
    Apr 30, 2015 at 18:45

1 Answer 1

6

You need to put the document.createElement part in the for loop. What you're doing now is creating the script tag once, add it to the document, and then changing the src a couple of times.

This does not work as you intended, you must create a new script element for each src. Example:

(function () {
    var lidd = 1;        
    var res = ["A1LpRTPOUKU%3d", "a8g%2bUPW0%2bck%3d", "ptb0PT3OMIc%3d", "8NzewxsG6Zc%3d"];
    for (var i = 0; i < res.length; i++) {
        var ga1 = document.createElement('script'); ga1.type = 'text/javascript';
        ga1.async = true;   
        alert('mid=' + res[i]);
        ga1.src = ('https:' == document.location.protocol ? 'https://ssl' :
        'http://') + 'localhost:59115/ui/trackconversion.aspx?cid=CAR9nmKJG1A%3d&mid=' + res[i] + '&lid=' + lidd;
        var s1 = document.getElementsByTagName('script')[0];
        s1.parentNode.insertBefore(ga1, s1);
    }
})();                  
2
  • 1
    Not the script creation part, only the adding to document part. I edited my answer for clarity.
    – Blaise
    Apr 30, 2015 at 10:04
  • 1
    I added a code example that demonstrates how you should do it.
    – Blaise
    Apr 30, 2015 at 10:08

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.