Jquery: Getting certain url path using window.location
Asked Answered
M

2

6

Our url pathname is

www.nicadpower.com/index.com

but we only want to get the pathname after www.nicadpower.com which is

index.com

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

Mump answered 18/6, 2011 at 4:19 Comment(0)
O
9

I think you want

window.location.pathname

This would give you /index.com

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

window.location.pathname.substring(1);
Ovolo answered 18/6, 2011 at 4:30 Comment(2)
console.log(window.location.pathname.substring(1)); putting this one on my js says that is getting www.nicadpower.com/index.phpMump
thanks ord! now is works good. i just added more integer on the substring. thanks a lot!Mump
N
2

Here is another trick that you can use. You can split the pathname parts using split method, like that:

If you have the url: www.nicadpower.com/posts/2012

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

You'll Give: ["", "posts", "2012"]

Novelia answered 26/9, 2012 at 19:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.