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);

You have three options:

  • escape()

     will not encode: 

    @*/+ will encode http:// to http%3A//
  • encodeURI()

     will not encode: 

    ~!@#$&*()=:/,;?+'
  • encodeURIComponent()

     will not encode: 

    ~!*()'

sources: http://stackoverflow.com/questions/332872/how-to-encode-a-url-in-javascript

admin