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

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

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

例子也可能改变。


当前回答

你可以很容易地得到它:

var currentUrl = window.location.href;

或者,如果你想要原始的URL,使用:

var originalUrl = window.location.origin;

其他回答

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

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+"://")]

拆分并加入URL:

const s = 'http://free-proxy.cz/en/abc'
console.log(s.split('/').slice(0,3).join('/'))
window.location.origin+"/"+window.location.pathname.split('/')[1]+"/"+page+"/"+page+"_list.jsp"

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

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/