JQuery: getting a specific url using window.location

Our URL

www.nicadpower.com/index.com 

but we only want to get the path after www.nicadpower.com, which

 index.com 

how can we get index.com using window.location and jquery

+6
source share
2 answers

I think you want

 window.location.pathname 

This will give you /index.com

To get rid of the leading /, you can simply use the substring:

 window.location.pathname.substring(1); 
+9
source

Here is another trick you can use. You can split the parts of the path using the split method, for example:

If you have a URL: www.nicadpower.com/posts/2012

Usage: window.location.pathname.split('/')

You will give: ["", "posts", "2012"]

+2
source

Source: https://habr.com/ru/post/890806/


All Articles