By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,046 Members | 1,350 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,046 IT Pros & Developers. It's quick & easy.

Javascript \ Escape problem

P: n/a
Greetings,

I have some javscript mixed into a applications templating and have
run into a problem the way javascript is handling a path for me.

I am via the template assigning a file path to a javascript variable.

The Tempalte: var myfile = "!FILEPATH!";
which becomes: var myfile = "C:\mydir\thefile.txt";

The issue of course being that the \'s are not escaped. I'm not as
fluent in Javascript as I'd like to be yet and I havn't figured out a
way to escape it so I can coninue on with the rest of the script.
I've tried to do a search and replace but javscript is already
interpreting the \'s as escaping a character.

How can I either autoescape the \'s (if possible), or access the raw
data so I can replace \ with \\?

Any help is appriciated.

Thanks!

BlueMac
Jul 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
> I am via the template assigning a file path to a javascript variable.

The Tempalte: var myfile = "!FILEPATH!";
which becomes: var myfile = "C:\mydir\thefile.txt";

The issue of course being that the \'s are not escaped. I'm not as
fluent in Javascript as I'd like to be yet and I havn't figured out a
way to escape it so I can coninue on with the rest of the script.
I've tried to do a search and replace but javscript is already
interpreting the \'s as escaping a character.

How can I either autoescape the \'s (if possible), or access the raw
data so I can replace \ with \\?


You have to do the \ correction before it gets handed to JavaScript.

Jul 20 '05 #2

P: n/a
BlueMac wrote:
I am via the template assigning a file path to a javascript variable.

The Tempalte: var myfile = "!FILEPATH!";
which becomes: var myfile = "C:\mydir\thefile.txt";

How can I either autoescape the \'s (if possible), or access the raw
data so I can replace \ with \\?


I'm assuming the JavaScript is being run inside an HTML document? If
so, and you know the browser will support the DOM standard, try

<meta name="FILEPATH" content="!FILEPATH!" />

in the <head>, and

var i, myfile;
var metas = document.getElementsByTagName("meta");

for (i = 0; i < metas.length; i++)
if (metas.item(i).getAttribute("name") == "FILEPATH") {
myfile = metas.item(i).getAttribute("content");
break;
}

in the script.

Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.