title: How do I URL Encode a URL parameter that is itself a URL?
body: <p>Quick background -</p>
  
  <p>I am making a jQuery ajax call to a service I wrote that returns a JSON response. The service accepts a web site URL (i.e. www.google.com, www.xyz.com/abc123). The format of the request is as follows:</p>
  
  <pre><code>http://www.mysite.com/[url]
  </code></pre>
  
  <p>... where [url] is a user provided URL (again, something like www.google.com/abc)</p>
  
  <p>I need to URL encode the parameter, as mysite.com/www.google.com is giving me errors.</p>
  
  <p>My problem is, all of the standard javascript encoding functionality does not actually encode the URL. </p>
  
  <p>An example:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;script&gt;
  document.write("encodeURIComponent = " + encodeURIComponent("www.google.com") + "&lt;br /&gt;");
  document.write("encodeURI = " + encodeURI("www.google.com") + "&lt;br /&gt;");
  document.write("escape = " + escape("www.google.com"));
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p></p>
  
  <p>... has the following output:</p>
  
  <pre><code>encodeURIComponent = www.google.com
  encodeURI = www.google.com
  escape = www.google.com
  </code></pre>
  
  <p>What is the proper way to achieve this using JavaScript/jQuery?</p>
  
  <p>I don't think it's relevant, but just in case, this is a Rails 3.0.7 app.</p>
  
  <p>EDIT FOR MORE DETAIL</p>
  
  <p>If www.google.com is already URL encoded, and periods are fine in my URL (www.mysite.com/www.google.com), why am I getting this error?</p>
  
  <p>From Chrome Dev Tools:</p>
  
  <pre><code>GET http://localhost:3000/s/www.google.com 404 (Not Found)
  </code></pre>
  
  <p>My jQuery snippet:</p>
  
  <pre><code>$.getJSON("http://localhost:3000/s/" + encodeURI($("#txtURL").val()), function(data) {
          alert(data.result.url);
      });
  </code></pre>