String manipulation in javascript?

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

    String manipulation in javascript?

    Hey all!

    New to javascript and still getting my head around strings...

    Consider the following line of code...

    var path = location.pathna me;

    ....after execution, the variable "path" contains something like
    "file:///C:/Documents%20and %20Settings/user/Desktop/Test/fileread.htm"

    How do I parse this down to "C:\Documen ts and Settings\user\D esktop\Test"
    ....or at least to "C:/Documents%20and %20Settings/user/Desktop/Test"

    Is there a better function to retrieve the source folder containing the
    current HTML document?

    I need to know the path to the current folder to reference other files in
    the same directory using a FileSystemObjec t.

    Thanks!


  • Ivo

    #2
    Re: String manipulation in javascript?

    "Phrederik" <postmaster@127 .0.0.1> wrote in message
    news:Dkrbb.7036 $I36.4705@pd7tw 3no...[color=blue]
    > Consider the following line of code...
    >
    > var path = location.pathna me;
    >
    > ...after execution, the variable "path" contains something like
    > "file:///C:/Documents%20and %20Settings/user/Desktop/Test/fileread.htm"
    >
    > How do I parse this down to "C:\Documen ts and Settings\user\D esktop\Test"
    > ...or at least to "C:/Documents%20and %20Settings/user/Desktop/Test"
    >
    > Is there a better function to retrieve the source folder containing the
    > current HTML document?
    >
    > I need to know the path to the current folder to reference other files in
    > the same directory using a FileSystemObjec t.
    >
    > Thanks!
    >[/color]

    Hi Phred,

    I used a different variable name because "path" looks too much like a
    reserved word to me.

    p="file:///C:/Documents%20and %20Settings/user/Desktop/Test/fileread.htm";
    alert( p = p.substring(p.i ndexOf('C:'), p.lastIndexOf('/')) );
    while( p.indexOf('/')+1 ) p=p.replace(/\//,'\\');
    while( p.indexOf('%20' )+1) p=p.replace(/%20/,' ');
    alert( p );

    Different string method are used here, to begin with indexOf and
    lastIndexOf. You can hardcode the string 'C:' only if you are certain
    everything will indeed be under C...
    These two methods return integers, numbers that is, which form the arguments
    for the third method, the substring, which cuts off the superfluous bits at
    start and end.
    The bit "p=p.substring( ...)" not only assigns a value to p, but also returns
    this value to any function willing to receive it. In this case there is one:
    the alert is which the whole is wrapped.
    Next two lines cycle through the variable, replacing matching substrings as
    they go. The first argument of the replace method is a regular expression.
    These are often delimited by slashes instead of quotes, and can have a
    global and case insensitive flag.
    Because it is slashes we are looking to replace, these must be escaped
    inside the regex string. Special characters are escaped by preceding them
    with a backslash. The backslash is also a special character.
    No alpha characters here, so case doesn't matter, and I opted for the while
    loop instead of the global flag for when you start using double spaces in
    your urls.
    The global flag doesn't catch immediate repeats.


    Comment

    • HikksNotAtHome

      #3
      Re: String manipulation in javascript?

      In article <Dkrbb.7036$I36 .4705@pd7tw3no> , "Phrederik" <postmaster@127 .0.0.1>
      writes:
      [color=blue]
      >
      >How do I parse this down to "C:\Documen ts and Settings\user\D esktop\Test"
      >...or at least to "C:/Documents%20and %20Settings/user/Desktop/Test"[/color]

      myVar = "file:///C:/Documents%20and %20Settings/user/Desktop/Test/fileread.htm"
      re = /\//g;
      myVar = unescape(myVar. replace('file:///','')).replace( re,'\\')
      myFile = myVar.lastIndex Of('\\')
      myPath = myVar.substring (0,myFile)
      alert(myPath)

      The alert gives me C:\Documents And Settings\user\D esktop\Test

      Probably a more efficient way, my knowledge of Regular Expressions is limited.
      Lightly tested in IE6.

      Note: On WinME, location.pathna me gives me
      /C:\WINDOWS\Desk top\webpage\bla nk.html
      Which is the path to the blank test file I used. Only needs the \\ changed to \
      and / removed from the beginning.

      Changing it to this:

      myVar = "file:///C:/Documents%20and %20Settings/user/Desktop/Test/fileread.htm"
      document.write( location.pathna me)
      re = /\//g;
      myVar = unescape(myVar. replace('file://','')).replace( re,'\\')
      myFile = myVar.lastIndex Of('\\')
      myPath = myVar.substring (1,myFile)
      alert(myPath)

      Seems to work when I set myVar the way its set, or, set it to location.pathna me
      --
      Randy

      Comment

      • Phrederik

        #4
        Re: String manipulation in javascript?

        Very helpful... Thanks!
        [color=blue]
        > myVar =[/color]
        "file:///C:/Documents%20and %20Settings/user/Desktop/Test/fileread.htm"[color=blue]
        > re = /\//g;
        > myVar = unescape(myVar. replace('file:///','')).replace( re,'\\')
        > myFile = myVar.lastIndex Of('\\')
        > myPath = myVar.substring (0,myFile)
        > alert(myPath)
        >
        > The alert gives me C:\Documents And Settings\user\D esktop\Test
        >
        > Probably a more efficient way, my knowledge of Regular Expressions is[/color]
        limited.[color=blue]
        > Lightly tested in IE6.
        >
        > Note: On WinME, location.pathna me gives me
        > /C:\WINDOWS\Desk top\webpage\bla nk.html[/color]

        You are correct... I failed to refresh the page before posting my message.
        [color=blue]
        > Which is the path to the blank test file I used. Only needs the \\ changed[/color]
        to \[color=blue]
        > and / removed from the beginning.
        >
        > Changing it to this:
        >
        > myVar =[/color]
        "file:///C:/Documents%20and %20Settings/user/Desktop/Test/fileread.htm"[color=blue]
        > document.write( location.pathna me)
        > re = /\//g;
        > myVar = unescape(myVar. replace('file://','')).replace( re,'\\')
        > myFile = myVar.lastIndex Of('\\')
        > myPath = myVar.substring (1,myFile)
        > alert(myPath)
        >
        > Seems to work when I set myVar the way its set, or, set it to[/color]
        location.pathna me

        Thanks again!


        Comment

        • Phrederik

          #5
          Re: String manipulation in javascript?

          Thanks for the info...

          I ended up looping through the string, but seeing the "indexOf" function,
          I'm going to change my code.

          I'm also using the unescape function to convert the "Url safe" text.

          "Ivo" <no@thank.you > wrote in message
          news:3f6e5fff$0 $35673$1b62eedf @news.wanadoo.n l...[color=blue]
          > "Phrederik" <postmaster@127 .0.0.1> wrote in message
          > news:Dkrbb.7036 $I36.4705@pd7tw 3no...[color=green]
          > > Consider the following line of code...
          > >
          > > var path = location.pathna me;
          > >
          > > ...after execution, the variable "path" contains something like
          > > "file:///C:/Documents%20and %20Settings/user/Desktop/Test/fileread.htm"
          > >
          > > How do I parse this down to "C:\Documen ts and[/color][/color]
          Settings\user\D esktop\Test"[color=blue][color=green]
          > > ...or at least to "C:/Documents%20and %20Settings/user/Desktop/Test"
          > >
          > > Is there a better function to retrieve the source folder containing the
          > > current HTML document?
          > >
          > > I need to know the path to the current folder to reference other files[/color][/color]
          in[color=blue][color=green]
          > > the same directory using a FileSystemObjec t.
          > >
          > > Thanks!
          > >[/color]
          >
          > Hi Phred,
          >
          > I used a different variable name because "path" looks too much like a
          > reserved word to me.
          >
          > p="file:///C:/Documents%20and %20Settings/user/Desktop/Test/fileread.htm";
          > alert( p = p.substring(p.i ndexOf('C:'), p.lastIndexOf('/')) );
          > while( p.indexOf('/')+1 ) p=p.replace(/\//,'\\');
          > while( p.indexOf('%20' )+1) p=p.replace(/%20/,' ');
          > alert( p );
          >
          > Different string method are used here, to begin with indexOf and
          > lastIndexOf. You can hardcode the string 'C:' only if you are certain
          > everything will indeed be under C...
          > These two methods return integers, numbers that is, which form the[/color]
          arguments[color=blue]
          > for the third method, the substring, which cuts off the superfluous bits[/color]
          at[color=blue]
          > start and end.
          > The bit "p=p.substring( ...)" not only assigns a value to p, but also[/color]
          returns[color=blue]
          > this value to any function willing to receive it. In this case there is[/color]
          one:[color=blue]
          > the alert is which the whole is wrapped.
          > Next two lines cycle through the variable, replacing matching substrings[/color]
          as[color=blue]
          > they go. The first argument of the replace method is a regular expression.
          > These are often delimited by slashes instead of quotes, and can have a
          > global and case insensitive flag.
          > Because it is slashes we are looking to replace, these must be escaped
          > inside the regex string. Special characters are escaped by preceding them
          > with a backslash. The backslash is also a special character.
          > No alpha characters here, so case doesn't matter, and I opted for the[/color]
          while[color=blue]
          > loop instead of the global flag for when you start using double spaces in
          > your urls.
          > The global flag doesn't catch immediate repeats.
          >
          >[/color]


          Comment

          • Phrederik

            #6
            Re: String manipulation in javascript?

            Yay!

            Got all my code working. The only problem I'm having now is the Internet
            Explorer warning that says:

            An ActiveX control on this page might be unsafe to
            interact with other parts of the page. Do you want to
            allow this interaction?

            ....are there security settings changes that can be made to avoid this
            message, without comprimising the browser security? The page will always be
            run from a CD in the drive and not from a web server.

            Thanks!

            "Phrederik" <postmaster@127 .0.0.1> wrote in message
            news:Dkrbb.7036 $I36.4705@pd7tw 3no...[color=blue]
            > Hey all!
            >
            > New to javascript and still getting my head around strings...
            >
            > Consider the following line of code...
            >
            > var path = location.pathna me;
            >
            > ...after execution, the variable "path" contains something like
            > "file:///C:/Documents%20and %20Settings/user/Desktop/Test/fileread.htm"
            >
            > How do I parse this down to "C:\Documen ts and Settings\user\D esktop\Test"
            > ...or at least to "C:/Documents%20and %20Settings/user/Desktop/Test"
            >
            > Is there a better function to retrieve the source folder containing the
            > current HTML document?
            >
            > I need to know the path to the current folder to reference other files in
            > the same directory using a FileSystemObjec t.
            >
            > Thanks!
            >
            >[/color]


            Comment

            Working...