开发者

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

开发者 https://www.devze.com 2023-04-09 09:30 出处:网络
I have generated inline code that assigns values to a javascript array.One of the elements of the ar开发者_C百科ray should be set to \"c:\\folder\\somefilename\".The statement is huge so I omitted mos

I have generated inline code that assigns values to a javascript array. One of the elements of the ar开发者_C百科ray 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


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.


Have you tried this?:

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消