0

In my javascript code I want to be able to retrieve current URL and then generate a new URL by adding relative paths (all of this without actually redirecting to new place, just want the URL string reference to pass on).

For eg: - this is exactly what I want but minus the redirection that kicks in

window.location.href = "../../";
var url = window.location.href;

I just want the result of appending relative path to current url and retrieve the new URL in string format.

EDIT with example to make the question more clear.

Suppose the current URL is:

www.example.com/level1/level2/level3/index.html

Now I want to get the URL 2 levels back:

www.example.com/level1
Raja
  • 6,354
  • 9
  • 30
  • 34
  • Well if you just want the reference you can do var url = window.location.href + "../../"; But I guess this is not what you really are asking? Not sure why redirection kicks in for you, might be missing some code – Mathijs Segers Apr 07 '15 at 14:29
  • yeah appending "../../" just gives me a string - i want to resolve that into a proper URL - href internally seems to do it but also redirects – Raja Apr 07 '15 at 14:31
  • 1
    What is the problem? question is not clear at all – charlietfl Apr 07 '15 at 14:32
  • say the current url in location bar is www.example.com/level1/level2/leve3/index.html Now I want to get a URL two levels back .i.e: www.example.com/level1 – Raja Apr 07 '15 at 14:33
  • @charlietfl The problem is clear. He wants `parsePath('http://example.com/lv1/lv2/lv3', '../../') -> 'http://example.com/lv1'` – Madara's Ghost Apr 07 '15 at 14:35
  • Short answer: Use an anchor element's `.href` instead of `document.location`'s. – Madara's Ghost Apr 07 '15 at 14:37
  • there is no anchor in my page - I just want to resolve based on my current location – Raja Apr 07 '15 at 14:40
  • Is it always a, I want to go back 2 directories action or do you want to apply a string path to the path in question? Still not clear. I'd know how to do exactly what you're asking here by splitting the url and removing the last 2 elements before imploding again... – Mathijs Segers Apr 08 '15 at 07:10

0 Answers0