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

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

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

例子也可能改变。


当前回答

这在javascript中是不可能的,因为这是一个服务器端属性。客户端上的Javascript无法知道joomla安装在哪里。最好的选择是在页面javascript中包含$this->baseurl的值,然后使用这个值(phpBaseUrl)。

然后你可以像这样构建url:

var loc = window.location;
var baseUrl = loc.protocol + "//" + loc.hostname + (loc.port? ":"+loc.port : "") + "/" + phpBaseUrl;

其他回答

这条会帮助你……

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

更简单的答案在这里,window。location。href = window。location。origin;

我认为这对你很有用:

var base_url = window.location.origin;

var host = window.location.host;

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

这是一个简短的例子:

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

var getUrl = window.location;
var baseurl = getUrl.origin; //or
var baseurl =  getUrl.origin + '/' +getUrl.pathname.split('/')[1]; 

但你不能说CodeIgniter(或php joomla)的baseurl()将返回相同的值,因为可以更改这些框架的.htaccess文件中的baseurl。

例如:

如果你在本地主机上有一个。htaccess文件:

RewriteEngine on
RewriteBase /CodeIgniter/
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

$this->baseurl()将返回http://localhost/CodeIgniter/