...

View Full Version : How to take apart an URL?



rechy_k
06-29-2006, 05:39 PM
Hi there,
I am new here...I read all the guidelines, searched the forum, but still couldn't find an answer.

I want to do the following:
-Read the URL of the current file
-replace the file name with a new file name
-open the new file
(everything is running currently on my local machine, so it comes back as: file://C:Documents\new\file.html, and I want it to look like C://Documents/new/file2.html)

My problem:
-Reading the file, the path has "\".
-To open a file, the path needs to have these "/"
-I am trying to replace them with a while loop, but whenever I use "\" to split the string or give me the index of its position, I get an error for invalvid character

Here is the piece of code so far... not even mentioning that I want to replace the file name...

function findPath()
{
var page = document.URL;
page = page.split("//");
var fullpath = page[1];
fullpath = fullpath.toString();
alert (fullpath);
//var c = fullpath.charAt(2); -> this puts out the \ just fine
//var c = fullpath.indexOf(\); -> this gives me invalid character
//alert (c);

//abc = fullpath.split("\"); -> this solution I would prefer, but gives me
also an invalid character message
//var i = 2;
//path = fullpath[1];
//while (i < fullpath.length)
// {
// path = path.concat("/",fullpath[i]);
// }

}

I appreciate any help
rechy

Bill Posters
06-29-2006, 06:45 PM
Maybe I'm missing something, but couldn't it be done simply by creating a regular anchor/link to the new file? The browser/server will replace the current document with the one specified in the link's href attribute when the link is clicked on.

If it still comes down to an issue over forward and backwards slashes, don't try to fix it to work with (or account for) backwards slashes. Just build the function as you would normally and know that it will work when served on a proper web server (which uses forward slashes).


Rather than dissect the url, you could alternatively just replace the piece you know…

e.g.

function goNew() {

window.location.href = window.location.href.replace('currentpage.html','newpage.html');

}

Alernatively, if you don't know the name of the current file…

function goNew() {

window.location.href = window.location.href.replace(window.location.href.split('/').pop(),'newpage.html');

}

Like I say, don't worry about the backwards slashes. The script will work when served properly.

rechy_k
06-29-2006, 07:00 PM
My problem is that the files names are stored in an array. Therefore the target file name is in a variable. And I couldn't get to work the anchor/link with a variable.

It looks like this

function pathNext()
{
var nextpos = nextPosition(current_topic_array, current_pos);
var filename = current_topic_array[nextpos];
path = path.concat(filename);
var news = window.open(path,"_self");
}


Otherwise my problem is that I am only writing this, but somebody else will put it on a web server, so how can I test it, without the server, if I shouldn't worry about the slashes.

Thanks
rechy

Bill Posters
06-29-2006, 07:11 PM
My problem is that the files names are stored in an array. Therefore the target file name is in a variable. And I couldn't get to work the anchor/link with a variable.
It's a fairly simple thing to swap out the 'newpage.html' reference in my example for a variable.


Otherwise my problem is that I am only writing this, but somebody else will put it on a web server, so how can I test it, without the server, if I shouldn't worry about the slashes.
Tbh, you should consider either installing proper web server software onto your machine or uploading the files to a remote server and testing them remotely (using a decent text editor to make remote alterations/tweaks).

If writing js is a job you've chosen to take on, then you need to make sure that both you and your facilities are up to the task. If you either can't write js and just know that it will work or can't be sure and can't test, then you should consider passing on the job for both your own sake and the sake of others involved in the chain of production.

rechy_k
06-29-2006, 07:23 PM
Thank you so much. I will give it a try... and will make sure that I can test appropriately.

It will take me a little...

rechy_k
06-29-2006, 08:48 PM
Thank you again for your quick help.

It is working fine now... it will take the variables.

rechy



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum