Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current url without page in javascript or jquery

How can I get current URL without page in Javascript or jQuery.

For example, if the url is:

http://www.abc.com/music/pop.aspx

I want to get the full path without the page so like:

http://www.abc.com/music/

No need to worry about parameters.

Thanks

like image 385
Laurence Avatar asked May 07 '13 11:05

Laurence


People also ask

How can get current URL with parameters in jQuery?

The current URL in jQuery can be obtained by using the 'href' property of the Location object which contains information about the current URL. The 'href' property returns a string with the full URL of the current page.

How do I get the current URL in HTML?

Answer: Use the window. location. href Propertyhref property to get the entire URL of the current page which includes host name, query string, fragment identifier, etc.

How do I get the URL of a website using JavaScript?

If you're using JavaScript in the browser you can get the full current URL by using window. location. href .


2 Answers

You can use substring() to extract the desired part of url.

Live Demo

urlBase = url.substring(0, url.lastIndexOf('/')+1);

You can use window.location.href to get the current url

urlBase = location.href.substring(0, location.href.lastIndexOf("/")+1)
like image 92
Adil Avatar answered Oct 12 '22 23:10

Adil


Use window.location and substring.

location.href.substring(0, location.href.lastIndexOf("/"))
like image 35
PurkkaKoodari Avatar answered Oct 12 '22 23:10

PurkkaKoodari