Javascript \ Escape problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • BlueMac

    Javascript \ Escape problem

    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\thefi le.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
  • Douglas Crockford

    #2
    Re: Javascript \ Escape problem

    > I am via the template assigning a file path to a javascript variable.[color=blue]
    >
    > The Tempalte: var myfile = "!FILEPATH! ";
    > which becomes: var myfile = "C:\mydir\thefi le.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 \\?[/color]

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

    Comment

    • David Leverton

      #3
      Re: Javascript \ Escape problem

      BlueMac wrote:[color=blue]
      > I am via the template assigning a file path to a javascript variable.
      >
      > The Tempalte: var myfile = "!FILEPATH! ";
      > which becomes: var myfile = "C:\mydir\thefi le.txt";
      >
      > How can I either autoescape the \'s (if possible), or access the raw
      > data so I can replace \ with \\?[/color]

      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="!FILEP ATH!" />

      in the <head>, and

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

      for (i = 0; i < metas.length; i++)
      if (metas.item(i). getAttribute("n ame") == "FILEPATH") {
      myfile = metas.item(i).g etAttribute("co ntent");
      break;
      }

      in the script.

      Comment

      Working...