Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 950
leymannx Avatar asked Aug 01 '13 11:08

leymannx


People also ask

How do I find the base URL?

To find the base URL of your website, go to the site's front page. What you see in the address bar on your site's front page is the base URL of your website.

How do I find the base URL in HTML?

The <base> tag specifies the base URL and/or target for all relative URLs in a document. The <base> tag must have either an href or a target attribute present, or both. There can only be one single <base> element in a document, and it must be inside the <head> element.

Which method returns the base URL for the object?

Returns the base URL for the object.

How do I find the base URL in python?

Pass the url to the urlparse method from the urllib. parse module. Access the netloc attribute on the parse result.


2 Answers

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.

like image 64
adeneo Avatar answered Sep 24 '22 15:09

adeneo


You can use this statement

var baseUrl = document.location.origin; 
like image 28
Peter T. Avatar answered Sep 21 '22 15:09

Peter T.