在joomla php中,我可以使用$this->baseurl来获得基本路径,但我想在jquery中获得基本路径。

基本路径可以是以下示例中的任意一个:

http://www.example.com/
http://localhost/example
http://www.example.com/sub/example

例子也可能改变。


当前回答

我认为这对你很有用:

var base_url = window.location.origin;

var host = window.location.host;

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

其他回答

把它放在你的标题中,这样当你需要它的时候就可以使用了。

var base_url = "<?php echo base_url();?>";

您将得到http://localhost:81/your-path-file或http://localhost/your-path-file。

window.location.origin+"/"+window.location.pathname.split('/')[1]+"/"+page+"/"+page+"_list.jsp"

几乎和Jenish的答案一样,但更短一点。

最简单的方法,以JavaScript获得基础url

window.location.origin

这将得到base url

var baseurl = window.location.origin+window.location.pathname;

这是一个简短的例子:

const base = new URL('/', location.href).href; console.log(基地);