title: JavaScript function to convert JSON key-value object to query string
body: <p>I'm working on formatting a URL for the <a href="http://developers.facebook.com/docs/reference/dialogs/feed/" rel="noreferrer">Facebook Feed Dialog</a>. There's so many parameters though. I want to have a function for these dialogs, something like:</p>
  
  <pre><code>function generateDialogUrl(dialog, params) {
    base  = "http://www.facebook.com/dialog/" + dialog + "?";
    tail = [];
    for (var p in params) {
      if (params.hasOwnProperty(p)) {
        tail.push(p + "=" + escape(params[p]));
      }
    }
    return base + tail.join("&amp;")
  }
  </code></pre>
  
  <p>Oh wow... I think I just answered my own question. Is that right? Is <code>escape()</code> the correct function to use?</p>
  
  <p>I'm stuck in the <a href="https://github.com/mattdipasquale/loversapp.com/blob/master/public/javascripts/lovers.js#L85" rel="noreferrer">Lovers source code</a>.</p>
  
  <p>UPDATE: Since, we're using jQuery, I rewrote the method using <code>jQuery.each</code>. I also replaced <code>escape()</code> with <code>encodeURIComponent()</code> as suggested by @galambalazs &amp; @T.J. Crowder. Thanks, guys!</p>
  
  <pre><code>var generateDialogUrl = function (dialog, params) {
    base  = "http://www.facebook.com/dialog/" + dialog + "?";
    tail = [];
    $.each(params, function(key, value) {
      tail.push(key + "=" + encodeURIComponent(value));
    })
    return base + tail.join("&amp;");
  }
  </code></pre>
  
  <p>It's working!</p>