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

Barmar
  • 741,623
  • 53
  • 500
  • 612
wallop
  • 2,510
  • 1
  • 22
  • 39

1 Answers1

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

Dimitar Popov
  • 686
  • 5
  • 12
  • 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 – wallop Mar 08 '16 at 23:05
  • Happy to help mate. If you come up with better solution, please post here for me and others to learn. – Dimitar Popov Mar 08 '16 at 23:06
  • btw this is what i am currently using. var hashParts = url.match(/#(\/.+)\?(.+)/i). hashPath = hashParts[1] and hashSearch = hashParts[2] http://jsbin.com/modagimipo/edit?html,js,console – wallop Mar 08 '16 at 23:12