Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7935523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:56:54+00:00 2026-06-03T21:56:54+00:00

How do I write a javascript function that takes one string argument which is

  • 0

How do I write a javascript function that takes one string argument which is a relative url as if it were a relative path such as you might see in a Linux bash script and combines it with the url of the current webpage to find a resource relative to the current webpage.

Examples of relative url:

  • file2.html
  • ./file2.html
  • ../foo/file2.html
  • ././file2.html

Example results:

  1. basic case

    • in webpage http://mysite.com/dir1/index.html
    • relative url foo.html
    • result http://mysite.com/dir1/foo.html
  2. excess current directory dots (and local webpage)

    • in webpage file:///C:/Users/Me/dir1/dir2/page.html
    • relative url ././foo.html
    • result file:///C:/Users/Me/dir1/dir2/foo.html
      • or second preference file:///C:/Users/Me/dir1/dir2/././foo.html
  3. parent directory (and some incidental excess dots, and another local webpage format)

    • in webpage file:///C|/Users/Me/dir1/dir2/anotherpage.html
    • relative url ./../././foo.html
    • result file:///C:/Users/Me/dir1/foo.html
      • or second preference file:///C:/Users/Me/dir1/dir2/./../././foo.html
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T21:56:57+00:00Added an answer on June 3, 2026 at 9:56 pm

    Here is the working all-purpose solution:

    function makePath(path, rel) {
        path = path.substring(0, path.lastIndexOf("/") + 1) + rel;
        var dirs = path.split("/");
        var result = [];
        for (var i = 0; i < dirs.length; i++) {
            if (dirs[i] == ".") continue;
            if (dirs[i] == "..") result.pop();
            else result.push(dirs[i]);
        }
        return result.join("/");
    }
    

    ​DEMO: http://jsfiddle.net/h22SA/

    Applying to the current page you should use:

    makePath(location.href, "././foo.html")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I write a function that takes a string denoting a natural number
How do I write a Javascript function that accepts a variable number of parameters,
I'm trying to write a javascript function that adds some DOM nodes to the
I wish to write a simple javascript function that toggles the visibility of a
I am trying to write a javascript function with an if statement that will
I need to write, in JavaScript (I am using jQuery), a function that is
During my routine work, i happened to write the chained javascript function which is
I tried to write some javascript that takes the entries of an array and
I'm trying to write a Java function that takes a Class<?> and returns a
I'm fairly new to Javascript/JQuery. I'm trying to write an app that takes text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.