0

I want current url path along with its query string.

I have tried below but not getting desired output.

var pathname = window.location.pathname; // Returns path only
var url = window.location.href;

Suppose my url is http://localhost:1111/Contact/Index?text=aa.

Note : query string is optional here if there is no query string then it should provide pathname only.

I want /Contact/Index?text=aa, TIA.

3
  • I got the answer its window.location.pathname+window.location.search.substr(1); Commented Sep 6, 2017 at 7:16
  • thanks guys for your efforts and time.. Commented Sep 6, 2017 at 7:21
  • window.location.search will do that for you. Commented Sep 6, 2017 at 7:38

2 Answers 2

0

In jQuery you can do

$(location).attr('href')

The location object also contains properties, like host, hash, protocol, and pathname.

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

6 Comments

So, why would you use that over location.href?
Because he asked for jQuery?
If I asked for a way to bake a cake with an industrial steel furnace, wouldn't you tell me to just use a normal oven?
No i woudn't. Because I would be curious how the cake tasted.
Rather "well done", probably.
|
-2

This window.location.pathname will give you the path or the url.

1 Comment

It does not return the query string, he also wants that