AV8R
AV8R

Reputation: 303

Javascript literal escaped in IE but not Firefox, Chrome or Safari

I have generated inline code that assigns values to a javascript array. One of the elements of the array should be set to "c:\folder\somefilename". The statement is huge so I omitted most for the sake of brevity:

<SCRIPT type="text/javascript">    
var StepsList = {"steps": [
    {
        "id": "RST4551509111516131001035411012110410849",
        "stepAudio": "",
        "screenImage": "IO1A.PNG",
        "demoNote": "",
         "controlRect": {
            "top": "297",
            "left": "256",
            "right": "277",
            "bottom": "318"
            },
        "valueText": "c:\uploadfile.txt",
        "label": "Presentation Server File"
    [snipped]
</SCRIPT>

The content of the valueText element, "c:\uploadfile.txt" is captured from user input prior to code generation. When this code is executed in Chrome, Firefox, IE5, IE6 or Safari, the literal is not parsed and simply assigned to the array correctly. IE7,8 and 9 throw an error "Expected hexadecimal digit" seeing the "\u" as an escape sequence.

I don't want javascript parsing my literals for escape characters. I have tried the old and putting it in a CDATA tag. Is there a work around for this problem?

TIA, Eddie

Upvotes: 1

Views: 848

Answers (3)

JAL
JAL

Reputation: 21563

This MSDN page indicates that the proper way to handle this in JScript (IE) is to use two backslashes.

Note: If you want to use the literal text \u in a string, then use two backslashes - (\u) — one to escape the first backslash.

Upvotes: 0

Ruan Mendes
Ruan Mendes

Reputation: 92294

If your string will contain a literal backslash, it must be escaped. For example:

{"valueText": "c:\\uploadfile.txt"}

If you're creating the JSON with a library on the server, it would automatically be escaped. That is, please don't generate JSON by hand.

Upvotes: 2

James Johnson
James Johnson

Reputation: 46067

Have you tried this?:

"valueText" : "c:\\uploadfile.txt"

Upvotes: 1

Related Questions