window.location
works 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?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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.
回答2:
You can use this statement
var baseUrl = document.location.origin;
回答3:
Try this:
location.protocol + "//" + location.host
回答4:
I think it will ok for you
var base_url = window.location.origin;
var host = window.location.host;
var pathArray = window.location.pathname.split( '/' );