I have a div that I'm using in a WYSIWYG editor, and when the user is done writing it gets submitted using an AJAX POST request. But I've been running into a problem: anytime the text contains an "&" character it would cut off everything after it. I spent way too much time looking at my server-side code, but the issue turned out to be on the client side (the whole URL encoding thing). So I looked into how to fix this on the JavaScript side, and came up with "encodeURIComponent". Based on the pages I read (links below), it is currently the best way to do URL encoding (escape is deprecated and encodeURI doesn't encode certain characters.
When are you supposed to use escape instead of encodeURI / encodeURIComponent? Should I use encodeURI or encodeURIComponent for encoding URLs?
But encodeURIComponent isn't working. Here's how I used it:
var wysiwyg = encodeURIComponent(document.getElementById("wysiwyg").innerHTML);
The contents of this div get submitted (so I know it's not my AJAX call), but it doesn't fix the "&" characters, and I haven't been able to find any alternatives on Google or SO. I've got a few shots in the dark about what could potentially be going on, but that's all they are (no research, just general guesses based on experience): - Maybe some kind of cross-browser wonkiness (not supported in Firefox or something stupid like that)? - Some other parameter I haven't found documentation for (maybe encoding types like UTF-8 or whatever)? - Some undocumented gotcha with using innerHTML?
So I'm grasping at straws here. Are there any known bugs with encodeURIComponent? Are there any alternatives available (apart from doing a ton of string replace work - which btw I'm actually considering if I can't get it to work)? Any workarounds, links, suggestions or clues would be greatly appreciated. Thanks.