0

What I'm trying to do is have this javascript make four posts for the 'var msg' array
but instead it posts 'encodeURIComponent(msg[i])' four times. How do I fix this?

   var msg = ['one', 
        'two', 
        'three', 
        'four' ];
        for (var i in msg) {   

        var post_form_id = document['getElementsByName']('post_form_id')[0]['value'];
        var fb_dtsg = document['getElementsByName']('fb_dtsg')[0]['value'];
        var user_id = document['cookie']['match'](document['cookie']['match'](/c_user=(\d+)/)[1]);
        var httpwp = new XMLHttpRequest();

        var urlwp = '/ajax/profile/composer.php?__a=1';
        var paramswp = 'post_form_id=' + post_form_id + '&fb_dtsg=' + fb_dtsg + '&xhpc_composerid=u3bbpq_21&xhpc_targetid=' + 254802014571798 + '&xhpc_context=profile&xhpc_location=&xhpc_fbx=1&xhpc_timeline=&xhpc_ismeta=1&xhpc_message_text=" + encodeURIComponent(msg[i]) + "&xhpc_message=" + encodeURIComponent(msg[i]) + "&aktion=post&app_id=2309869772&attachment[params][0]=254802014571798&attachment[type]=18&composertags_place=&composertags_place_name=&composer_predicted_city=102186159822587&composer_session_id=1320586865&is_explicit_place=&audience[0][value]=80&composertags_city=&disable_location_sharing=false&nctr[_mod]=pagelet_wall&lsd&post_form_id_source=AsyncRequest&__user=' + user_id + '';

               {
        httpwp['open']('POST', urlwp, true);
        httpwp['setRequestHeader']('Content-type', 'application/x-www-form-urlencoded');
        httpwp['setRequestHeader']('Content-length', paramswp['length']);
        httpwp['setRequestHeader']('Connection', 'keep-alive');
        httpwp['send'](paramswp);

        i += 1;
                        }
         }   
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
  • Why are you doing `document['getElementsByName']`? That's terrible coding style. You should be using `document.getElementsByName`. – Eric Nov 08 '11 at 08:05
  • You have a mess of single and double quotes in your `var paramswp = ...` line, so the method call is seen as literal string. You can easily see it above in your question because of syntax colouring. – ivantod Nov 08 '11 at 08:07
  • I was beat to the answer, both are correct, however might I suggest avoiding such long one liners in the future. Maybe break it up into more variables even if it is for the sake of readability. – Darren Reid Nov 08 '11 at 08:16

2 Answers2

2

At this point you are switching from single to double quotes:

&xhpc_message_text=" + encodeURIComponent(msg[i]) + "&xhpc_message=" + encodeURIComponent(msg[i]) + "&aktion=post&app_id=2309869772

Try using single quotes instead, and it should be parsed correctly.

kasimir
  • 1,506
  • 1
  • 20
  • 26
0

Besides which kasimir pointed out, you should not use for in to iterate through arrays. Change your code to for (var i = 0, nMsg = msg.length; i < nMsg; ++i) and remove line i = i + 1

Community
  • 1
  • 1
ExpExc
  • 3,828
  • 1
  • 15
  • 20