2

Using jQuery 1.9.1 in IE8 and Firefox. Primary environment would be for IE8.

The web page I'm working on has a textarea where a user can type in text & upon a submit button click, is formatted to XML, and written to a table via an AJAX POST. It calls a stored procedure which accepts XML input.

I've tried several different ways, but can't get the following text to not throw an error when the stored procedure tries to write it to the table. The text is:

jQuery UI is a curated set of user interface interactions, effects, widgets, and themes built on top of the jQuery JavaScript Library. Whether you're building highly interactive web applications or you just need to add a date picker to a form control, jQuery UI is the perfect choice.

The stored procedure says it has "incorrect syntax near 're'".

I've tried 3 ways to encode the contents of the textarea, but all do the same thing & throw the same error.

I have tried:

encodeURIComponent( texareaMsg );
encodeURI (textareaMsg);

and also

htmlEncode(textareaMsg);

(where htmlEncode is the following function)

function htmlEncode(value) {
    return $("<div />").text(value).html();
}

It doesn't appear that any of them are escaping/encoding the apostrophe. The encodeURIComponent returns:

jQuery%20UI%20is%20a%20curated%20set%20of%20user%20interface%20interactions%2C%20effects%2C%20widgets%2C%20and%20themes%20built%20on%20top%20of%20the%20jQuery%20JavaScript%20Library.%20Whether%20you're%20building%20highly%20interactive%20web%20applications%20or%20you%20just%20need%20to%20add%20a%20date%20picker%20to%20a%20form%20control%2C%20jQuery%20UI%20is%20the%20perfect%20choice.

The encodeURI returns:

jQuery%20UI%20is%20a%20curated%20set%20of%20user%20interface%20interactions,%20effects,%20widgets,%20and%20themes%20built%20on%20top%20of%20the%20jQuery%20JavaScript%20Library.%20Whether%20you're%20building%20highly%20interactive%20web%20applications%20or%20you%20just%20need%20to%20add%20a%20date%20picker%20to%20a%20form%20control,%20jQuery%20UI%20is%20the%20perfect%20choice.

the htmlEncode function returns:

jQuery UI is a curated set of user interface interactions, effects, widgets, and themes built on top of the jQuery JavaScript Library. Whether you're building highly interactive web applications or you just need to add a date picker to a form control, jQuery UI is the perfect choice.

How are you supposed to encode/escape things like the apostrophe in this case (and other special characters) that is causing this problem? Again - I have to develop for the primary environment of IE8. What I've found thus far hasn't worked.

EDIT

@Krishna's comment helped point me in a direction that I was able to get it working to some degree. I haven't been able to blow it up with the test data yet. Not sure if it is the best solution either.

var myString = msgArr[0].MsgText;
myString = myString.replace(/ & /g, "&amp;");
myString = myString.replace(/"/g, "&quot;");
myString = myString.replace(/'/g, "&apos;");
myString = myString.replace(/</g, "&lt;");
myString = myString.replace(/>/g, "&gt;");

then later on when I needed to put that into the XML I was creating, I did this:

myXML.find("Msg").text( encodeURIComponent(myString)); 

I say it's probably not the best solution, because without putting a space in the replace for the ampersand(&), it corrupted other replacements in the string.

4

1 回答 1

2

您还可以使用正则表达式作为替换特殊字符的通用方法escape

var myString = msgArr[0].MsgText;
myString.replace(/[!'()*&"<>]/g, escape);

例如:

"you're".replace(/[!'()*&"<>]/g, escape)

输出:"you%27re"

于 2013-10-15T15:59:57.640 回答