最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - JavaScript: A BackSlash as part of the string - Stack Overflow

programmeradmin2浏览0评论

I have a JavaScript variable that I echo out using PHP which is shown like this in the page source:

var db_1 = 'C:\this\path';

When I set the value of a text field with that variable like so:

$('#myinput').val(db_1);

The slashes have disappeared and only the other characters are left!

Why is this and how can I put the slashes back in??

Thanks all

I have a JavaScript variable that I echo out using PHP which is shown like this in the page source:

var db_1 = 'C:\this\path';

When I set the value of a text field with that variable like so:

$('#myinput').val(db_1);

The slashes have disappeared and only the other characters are left!

Why is this and how can I put the slashes back in??

Thanks all

Share Improve this question asked Apr 19, 2010 at 15:14 AbsAbs 57.9k103 gold badges281 silver badges416 bronze badges 0
Add a comment  | 

3 Answers 3

Reset to default 11

A backslash is an escape character in JS. They are lost when the string literal is parsed.

You can't put them back, because you have no way of telling where they were. You have to make sure they remain in the string in the first place (by representing them with an escape sequence).

var db_1 = 'C:\\this\\path';

You can use:

echo json_encode('C:\this\path');

json_encode can be used as a filter function for some JavaScript code.

Try this:

var db_1 = 'C:\\this\\path';

For more info: http://www.w3schools.com/js/js_special_characters.asp

发布评论

评论列表(0)

  1. 暂无评论