Cassandra Cassandra - 1 year ago 136
Javascript Question

Javascript - how to remove domain from location.href

I need to remove the domain name from location.href using Javascript. I have links like:

and I need to have links without
and in future without it's real domain name.

I will use those trimed links in Javascript function so I would like to trim it also in Javascript.

I have tried:
but I could only get domain form it. And I want to get rid of domain.

Any help here much appreciated!

Answer Source

Use window.location.pathname. This gives you the path relative to the host. See here for more details.

For any arbitrary URL, assuming that the variable url contains your URL, you can do:

url = url.replace(/^.*\/\/[^\/]+/, '')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download