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

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

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

例子也可能改变。


当前回答

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

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

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

其他回答

您提到example.com可能会更改,因此我怀疑实际上您需要基本url,以便能够为脚本使用相对路径表示法。在这种特殊情况下,不需要使用脚本-而是添加base标签到你的头:

<head>
  <base href="http://www.example.com/">
</head>

我通常通过PHP生成链接。

有同样的问题前一段时间,我的问题是,我只是需要基础url。这里有很多详细的选项,但要解决这个问题,只需使用窗口。位置的对象。实际上,在浏览器控制台中输入这个,然后按enter键选择你的选项。我的情况很简单:

window.location.origin

这条会帮助你……

var getUrl = window.location;
var baseUrl = getUrl .protocol + "//" + getUrl.host + "/" + getUrl.pathname.split('/')[1];

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

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

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

这里有一些也适用于file:// url的快速方法。

我想出了这样一句话:

[((1!=location.href.split(location.href.split("/").pop())[0].length?location.href.split(location.href.split("/").pop())[0]:(location.protocol,location.protocol+"//" + location.host+"/"))).replace(location.protocol+"//"+location.protocol+"//"+location.protocol+"://")]