<p>How do you safely encode a URL using JavaScript such that it can be put into a GET string?</p>

<pre><code>var myUrl = "http://example.com/index.html?param=1&amp;anotherParam=2";
var myOtherUrl = "http://example.com/index.html?url=" + myUrl;
</code></pre>

<p>I assume that you need to encode the <code>myUrl</code> variable on that second line?</p>

Check out the built-in function
encodeURIComponent(str)
and
encodeURI(str)
.
In your case, this should work:
```
```
var myOtherUrl = 
       "http://example.com/index.html?url=" + encodeURIComponent(myUrl);
```
```