473,385 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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.pathname;

....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:\Documents and Settings\user\Desktop\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 FileSystemObject.

Thanks!
Jul 20 '05 #1
5 18192
Ivo
"Phrederik" <po********@127.0.0.1> wrote in message
news:Dkrbb.7036$I36.4705@pd7tw3no...
Consider the following line of code...

var path = location.pathname;

...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:\Documents and Settings\user\Desktop\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 FileSystemObject.

Thanks!


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.indexOf('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.
Jul 20 '05 #2
In article <Dkrbb.7036$I36.4705@pd7tw3no>, "Phrederik" <po********@127.0.0.1>
writes:

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


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

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

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

Note: On WinME, location.pathname gives me
/C:\WINDOWS\Desktop\webpage\blank.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.pathname)
re = /\//g;
myVar = unescape(myVar.replace('file://','')).replace(re,'\\')
myFile = myVar.lastIndexOf('\\')
myPath = myVar.substring(1,myFile)
alert(myPath)

Seems to work when I set myVar the way its set, or, set it to location.pathname
--
Randy
Jul 20 '05 #3
Very helpful... Thanks!
myVar = "file:///C:/Documents%20and%20Settings/user/Desktop/Test/fileread.htm" re = /\//g;
myVar = unescape(myVar.replace('file:///','')).replace(re,'\\')
myFile = myVar.lastIndexOf('\\')
myPath = myVar.substring(0,myFile)
alert(myPath)

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

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

Note: On WinME, location.pathname gives me
/C:\WINDOWS\Desktop\webpage\blank.html
You are correct... I failed to refresh the page before posting my message.
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.pathname)
re = /\//g;
myVar = unescape(myVar.replace('file://','')).replace(re,'\\')
myFile = myVar.lastIndexOf('\\')
myPath = myVar.substring(1,myFile)
alert(myPath)

Seems to work when I set myVar the way its set, or, set it to

location.pathname

Thanks again!
Jul 20 '05 #4
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:3f***********************@news.wanadoo.nl...
"Phrederik" <po********@127.0.0.1> wrote in message
news:Dkrbb.7036$I36.4705@pd7tw3no...
Consider the following line of code...

var path = location.pathname;

...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:\Documents and Settings\user\Desktop\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 FileSystemObject.

Thanks!

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.indexOf('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.

Jul 20 '05 #5
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" <po********@127.0.0.1> wrote in message
news:Dkrbb.7036$I36.4705@pd7tw3no...
Hey all!

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

Consider the following line of code...

var path = location.pathname;

...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:\Documents and Settings\user\Desktop\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 FileSystemObject.

Thanks!

Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Sean Williams | last post by:
I have been working in this problem for weeks and I was determined to sort it myself however I feel I need guidance to proceed forward. What I am trying to produce is some asp code that can split...
3
by: Fabian | last post by:
I have created a javascript to manipulate a text strong given to it. It works in all the situations I put it in. Now, I want to create a form based interface. Essentially, the use types in the text...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
4
by: WaterWalk | last post by:
Hello, I'm currently learning string manipulation. I'm curious about what is the favored way for string manipulation in C, expecially when strings contain non-ASCII characters. For example, if...
2
by: Anat | last post by:
Hi, I need a little help on performing string manipulation: I want to take a given string, and make certain words hyperlinks. For example: "Hello world, this is a wonderful day!" I'd like the...
9
by: zacariaz | last post by:
I dont know, and i dont much like javascript, however, i am told that the only way to do want i want to do, is with javascript, so here goes. zoom and cut is the only features i need. 1. the...
12
by: Andy McNamara | last post by:
Very simple questions, I have some data of strings, say, str1,sting2,stringstring3,etc I also have a text file that needs to be filled upat some empty places, I need to do this in javascript,...
52
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
3
by: Henri | last post by:
Hi, How would one go about comparing 2 strings one of which may contain special entities (eg "cassé" and "cassé")? I tried to find a way to take the second string and do a replace whenever such...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.