4

For a url such as

http://example.com/x1/x2?qp1a=val1&qp1b=val2#/y1/y2?qp2a=val1b&qp2b=val2b

location.pathname = x1/x2 and location.search = ?qp1a=val1&qp1b=val2

how to get y1/y2 and ?qp2a=val1b&qp2b=val2b USING window.location

Note: I know to solve this using reg ex and other ways, i am more interested in knowing how to get these values using window.location

6
  • Seems like you'll want the split() method of string. Commented Mar 8, 2016 at 22:56
  • @ScottMarcus i am looking for window.location helpers mainly thanks. Commented Mar 8, 2016 at 23:02
  • I don't see how you are going to get around using split(). Commented Mar 9, 2016 at 14:49
  • @ScottMarcus thats why the question in stackoverflow : ), you can still use regex, see my comment in the answer section Commented Mar 9, 2016 at 16:35
  • split actually uses an implied RegEx as its argument and is usually simpler than explicit RegEx. Commented Mar 9, 2016 at 16:41

1 Answer 1

2

location.hash gets the anchor part of a URL!

In your case it will be #/y1/y2?qp2a=val1b&qp2b=val2b

Then you can remove the leading hashtag using string.substring() and split on ? sign using string.split() in order to get the two strings - /y1/y2 and qp2a=val1b&qp2b=val2b

Sign up to request clarification or add additional context in comments.

3 Comments

a simple url.split('#')[1] would provide me the same. I am already using a regular expression to match what i want i was just wondering if i could get a window.location which could provide me the pathname and qp better but hash is something that i can use to optimize slightly. thanks
Happy to help mate. If you come up with better solution, please post here for me and others to learn.
btw this is what i am currently using. var hashParts = url.match(/#(\/.+)\?(.+)/i). hashPath = hashParts[1] and hashSearch = hashParts[2] jsbin.com/modagimipo/edit?html,js,console

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.