norman.lol
norman.lol

Reputation: 5374

How to get the base path in jQuery?

window.locationworks fine, but returns me the whole, absolute path, like http://domain.xyz/punch/lines. But I only need http://domain.xyz/. How can I extract only that first part? And how can I make that dynamic, I mean to be always the same even when the subdirectory path gets longer?

Upvotes: 37

Views: 103711

Answers (4)

Haris N I
Haris N I

Reputation: 6844

I think it will ok for you

var base_url = window.location.origin;

var host = window.location.host;

var pathArray = window.location.pathname.split( '/' );

Upvotes: 2

Peter Tadros
Peter Tadros

Reputation: 9297

You can use this statement

var baseUrl = document.location.origin;

Upvotes: 29

adeneo
adeneo

Reputation: 318182

You can get the protocol and the host separately, and then join them to get what you need

window.location.protocol + "//" + window.location.host + "/"

As a sidenote, window.location.pathname would contain the path.

Upvotes: 80

Dharmesh Patel
Dharmesh Patel

Reputation: 1891

Try this:

location.protocol + "//" + location.host

Upvotes: 3

Related Questions