Javascript \ Escape problem

B

BlueMac

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
 
D

Douglas Crockford

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.
 
D

David Leverton

BlueMac said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top