且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从JavaScript中的字符串中提取基本URL?

更新时间:2023-02-23 16:00:38

编辑:有人抱怨它没有考虑协议。所以我决定升级代码,因为它被标记为答案。对于那些喜欢单行代码的人...很抱歉这就是为什么我们使用代码最小化,代码应该是人类可读的,这种方式更好......在我看来。

Some complain that it doesn't take into account protocol. So I decided to upgrade the code, since it is marked as answer. For those who like one-line-code... well sorry this why we use code minimizers, code should be human readable and this way is better... in my opinion.

var pathArray = location.href.split( '/' );
var protocol = pathArray[0];
var host = pathArray[2];
var url = protocol + '//' + host;

或使用来自下方的戴维斯解决方案。

Or use Davids solution from below.