473,729 Members | 2,215 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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!
Jul 20 '05 #1
5 18206
Ivo
"Phrederik" <po********@127 .0.0.1> wrote in message
news:Dkrbb.7036 $I36.4705@pd7tw 3no...
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!


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.
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:\Documen ts and Settings\user\D esktop\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.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
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.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
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.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

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.n l...
"Phrederik" <po********@127 .0.0.1> wrote in message
news:Dkrbb.7036 $I36.4705@pd7tw 3no...
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!

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.

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@pd7tw 3no...
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!

Jul 20 '05 #6

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

Similar topics

0
1560
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 categories and group from a text string that is delimited by a "| ". (see string examples below). I have managed to find some javascript that will handle the different levels of categories without having to keep refreshing the page. So I want...
3
3033
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 in a form text box, and it returns the manipulated text. Ideally, I'd like it to return teh text in teh same page, without reloading anything from the server. Problem is, I have no idea how to capture anything entered into a form with...
32
14880
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 ((someString.IndexOf("something1",0) >= 0) || ((someString.IndexOf("something2",0) >= 0) ||
4
3489
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 substrings need be replaced, or one character needs be changed, what shall I do? Is it better to convert strings to UCS-32 before manipulation? But on Windows, wchar_t is 16 bits which isn't enough for characters which can't be simply encoded...
2
2215
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 words world & and day to be hyperlinks, therefore after my manipulation it should be: "Hello <a href=...>world</a>, this is a wonderful <a href=...>day</a>!" Using split method is not good, because splitting with regex each punctuation mark causes...
9
3376
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 image that need manipulating is places on the server. dont need js for that ;-) 2. the client has the oppotunity to manipulate the image. cut and zoom. 3. the image i saved on the server.
12
2580
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, could you help me with some apis that can do this ? I am completely new to javascript. any help is appreciated.
52
1565
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places? ----------------------------------------------------------------------- When formatting money for example, to format 6.57634 to 6.58, 6.5 to 6.50, and 6 to 6.00? Rounding of x.xx5 is uncertain, as such numbers are not represented exactly.
3
10702
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 entities are encountered but this implies creating some sort of lookup table containing not all but a good number of entity codes. Unless I am mistaken, javascript does not any function to replace an entity-infested string with a decoded string,...
0
8931
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8767
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9428
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8165
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6028
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4799
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3246
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2701
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2171
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.