title: jQuery append params to url
body: <p>I like the way jQuery's $.ajax() method allows to specify request url:</p>
  
  <pre><code>{
    url: 'http://domain.com/?param=1',
    data{
      param2: '2'
    }
  }
  </code></pre>
  
  <p>$.ajax() method will (probably) call $.param() on provided data and optionally append it to provided URL.</p>
  
  <p>My question is: is this type of url manipulation available outside of $.ajax() call?</p>
  
  <p>For example, I want to open a popup window, and I would like to construct URL in the same way that I do with $.ajax().</p>
  
  <p>I have written a function which does this, but I have a feeling I am reinventing the wheel and duplicating already existing function of jQuery:</p>
  
  <pre><code>var prepareUrl = function( url, data )
  {
    var params = $.param( data );
  
    if ( params.length &gt; 0 )
    {
      // url contains a query string
      if ( url.indexOf( '?' ) &gt; -1 )
      {
        // get last char of url
        var lastChar = url.substr( url.length - 1 );
  
        // Append &amp; to the end of url if required
        if ( lastChar != '&amp;' &amp;&amp; lastChar != '?' )
        {
          url += '&amp;';
        }
      }
  
      else // url doesn't contain a query string
      {
        url += '?';
      }
  
      url += params;
    }
  
    return url;
  }
  </code></pre>
  
  <p>thanks!</p>