maintaining characters that are not escaped by javascript escape() function


I have a bit of javascript/jquery code that takes a long string, and sends it via an AJAX request to a php CodeIgniter function which processes it further. The call looks like this:

var params = "str="+escape(JSON.stringify($('#element').find('html').get(0).outerHTML));
xmlhttp1 = new XMLHttpRequest();
xmlhttp1.open("POST", "http://www.url.com/controller/function", false);
xmlhttp1.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp1.setRequestHeader("Content-length", params.length);
xmlhttp1.setRequestHeader("Connection", "close");
xmlhttp1.send(params);

It passes the desired outerHTML just fine, however the escape() fails to escape certain characters like '+' which are necessary to the php function. Is there another function available to escape these characters or do I need to manually process the outerHTML before sending it to the php function?

I believe you should be using encodeURIComponent instead of escape:

var params = "str=" + encodeURIComponent(JSON.stringify($('#element').find('html').get(0).outerHTML));