Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to take apart an URL?

    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

  • #2
    Senior Coder
    Join Date
    Feb 2003
    Posts
    1,665
    Thanks
    0
    Thanked 27 Times in 25 Posts
    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.
    Code:
    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…
    Code:
    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.

  • #3
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to take apart an URL

    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

  • #4
    Senior Coder
    Join Date
    Feb 2003
    Posts
    1,665
    Thanks
    0
    Thanked 27 Times in 25 Posts
    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.

  • #5
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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...

  • #6
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you again for your quick help.

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

    rechy


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •