在joomla php中,我可以使用$this->baseurl来获得基本路径,但我想在jquery中获得基本路径。
基本路径可以是以下示例中的任意一个:
http://www.example.com/
http://localhost/example
http://www.example.com/sub/example
例子也可能改变。
在joomla php中,我可以使用$this->baseurl来获得基本路径,但我想在jquery中获得基本路径。
基本路径可以是以下示例中的任意一个:
http://www.example.com/
http://localhost/example
http://www.example.com/sub/example
例子也可能改变。
当前回答
格式为“hostname/pathname/search”
url是:
var url = window.location.hostname + window.location.pathname + window.location.hash
为了你的案子
window.location.hostname = "stackoverflow.com"
window.location.pathname ="/questions/25203124/how-to-get-base-url-with-jquery-or-javascript"
window.location.hash = ""
baseurl = hostname = window。location。hostname
其他回答
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/
这将得到base url
var baseurl = window.location.origin+window.location.pathname;
我在几个Joomla项目中遇到过这种需求。我发现的最简单的方法是在模板中添加一个隐藏的输入字段:
<input type="hidden" id="baseurl" name="baseurl" value="<?php echo $this->baseurl; ?>" />
当我需要的值在JavaScript:
var baseurl = document.getElementById('baseurl').value;
不像使用纯JavaScript那样花哨,但很简单,可以完成工作。
文档。baseURI返回的基本URL也与<base/>标签中的值有关 https://developer.mozilla.org/en-US/docs/Web/API/Node/baseURI