开发者

JavaScript: A BackSlash as part of the string

开发者 https://www.devze.com 2022-12-27 04:22 出处:网络
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\\开发者_Python百科path\';

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\开发者_Python百科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


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

精彩评论

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