在joomla php中,我可以$this->baseurl
用来获取基本路径,但是我想在jquery中获取基本路径。
基本路径可以是以下任何示例:
http://www.example.com/
http://localhost/example
http://www.example.com/sub/example
该example
也可能改变。
这将帮助您...
var getUrl = window.location;
var baseUrl = getUrl .protocol + "//" + getUrl.host + "/" + getUrl.pathname.split('/')[1];
我想你会没事的
var base_url = window.location.origin;
var host = window.location.host;
var pathArray = window.location.pathname.split( '/' );
这将获得基本URL
var baseurl = window.location.origin+window.location.pathname;
这是一个非常老的问题,但这是我个人使用的方法...
获取标准/基本URL
正如许多人已经指出的那样,这适用于大多数情况。
var url = window.location.origin;
获取绝对基本URL
但是,可以使用这种简单方法剥离所有端口号。
var url = "http://" + location.host.split(":")[0];
编辑:为了解决由Jason Rice提出的问题,可以使用以下自动插入正确的协议类型...
var url = window.location.protocol + "//" + location.host.split(":")[0];
设置基本URL
另外,基本URL可以全局重新定义。
document.head.innerHTML = document.head.innerHTML + "<base href='" + url + "' />";
document.baseURI
返回基本URL,同时也遵守<base/>
标记
https://developer.mozilla.org/en-US/docs/Web/API/Node/baseURI中的值
使用javascript不可能,因为这是服务器端属性。客户端上的Javascript无法知道joomla的安装位置。最好的选择是以某种方式将$this->baseurl
javascript的值包含到页面javascript中,然后使用此值(phpBaseUrl
)。
然后,您可以像这样构建网址:
var loc = window.location;
var baseUrl = loc.protocol + "//" + loc.hostname + (loc.port? ":"+loc.port : "") + "/" + phpBaseUrl;
前段时间有同样的问题,我的问题是,我只需要基本URL。这里有很多详细的选项,但是要解决这个问题,只需使用window.location
对象。实际上,在浏览器控制台中键入此内容,然后按Enter键以在那里选择您的选项。对我来说,这很简单:
window.location.origin
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 /
在jQuery中获取基本URL的最简单方法
window.location.origin
我已经在几个Joomla项目中遇到了这种需求。我发现要解决的最简单方法是在模板中添加一个隐藏的输入字段:
<input type="hidden" id="baseurl" name="baseurl" value="<?php echo $this->baseurl; ?>" />
当我需要JavaScript中的值时:
var baseurl = document.getElementById('baseurl').value;
不像使用纯JavaScript那样花哨,而是简单并且可以完成工作。
您可以通过以下方式轻松获得它:
var currentUrl = window.location.href;
或者,如果您想要原始URL,请使用:
var originalUrl = window.location.origin;
我建议所有人在开发中创建HTML基本标记,然后动态分配href,因此在生产中,无论客户端使用什么主机,它都会自动对其进行自适应:
<html>
<title>Some page title</titile>
<script type="text/javascript">
var head = document.getElementsByTagName('head')[0];
var base = document.createElement("base");
base.href = window.document.location.origin;
head.appendChild(base);
</script>
</head>
...
因此,如果您在localhot:8080中,那么您将从基础访问每个链接或引用的文件,例如:http://localhost:8080/some/path/file.html
如果您在www.example.com中,它将是http://www.example.com/some/path/file.html
另请注意,在您所处的每个位置上,都不应在hrefs中使用globs之类的引用,例如:父位置导致http://localhost:8080/
not http://localhost:8080/some/path/
。
伪装您将所有超链接引用为完整的句子,没有bas 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
令我惊讶的是,如果答案是在<base>
标签中设置的,则没有答案会考虑基本网址。当前所有答案都尝试获取主机名或服务器名或地址的第一部分。这是完整的逻辑,还考虑了<base>
标记(可能引用另一个域或协议):
function getBaseURL(){
var elem=document.getElementsByTagName("base")[0];
if (typeof(elem) != 'undefined' && elem != null){
return elem.href;
}
return window.location.origin;
}
jQuery格式:
function getBaseURL(){
if ($("base").length){
return $("base").attr("href");
}
return window.location.origin;
}
在不涉及上述逻辑的情况下,速记解决方案同时考虑了<base>
tag和window.location.origin
:
Js:
var a=document.createElement("a");
a.href=".";
var baseURL= a.href;
jQuery:
var baseURL= $('<a href=".">')[0].href
最后说明:对于计算机中的本地文件(不在主机上),window.location.origin
仅返回,file://
但是上面的排序解决方案返回完整的正确路径。
window.location.origin+"/"+window.location.pathname.split('/')[1]+"/"+page+"/"+page+"_list.jsp"
几乎与Jenish答案相同,但略短一些。
我正处于同一阶段,此解决方案对我有用
在视图中
<?php
$document = JFactory::getDocument();
$document->addScriptDeclaration('var base = \''.JURI::base().'\'');
$document->addScript('components/com_name/js/filter.js');
?>
在js文件中,您可以base
在场景中将其作为变量进行访问:
console.log(base) // will print
// http://www.example.com/
// http://localhost/example
// http://www.example.com/sub/example
我不记得我在哪里获得这些信息,如果找到了,我将编辑答案。
这是一个简短的:
const base = new URL('/', location.href).href;
console.log(base);
简单
$('<img src=>')[0].src
生成带有空src-name的img会强制浏览器自行计算基本URL,无论您是否拥有它/index.html
或其他任何东西。
var getUrl = window.location;
var baseUrl = getUrl .protocol + "//" + getUrl.host + "/" + getUrl.pathname.split('/')[1];
这是一些快速操作,也可用于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+"://")]
您提到example.com
可能会更改,因此我怀疑实际上您仅需要基本URL才能够为脚本使用相对路径表示法。在这种特殊情况下,无需使用脚本-而是将基本标记添加到标头中:
<head>
<base href="http://www.example.com/">
</head>
我通常通过PHP生成链接。
万一有人想看到它分解成一个非常强大的功能
function getBaseURL() {
var loc = window.location;
var baseURL = loc.protocol + "//" + loc.hostname;
if (typeof loc.port !== "undefined" && loc.port !== "") baseURL += ":" + loc.port;
// strip leading /
var pathname = loc.pathname;
if (pathname.length > 0 && pathname.substr(0,1) === "/") pathname = pathname.substr(1, pathname.length - 1);
var pathParts = pathname.split("/");
if (pathParts.length > 0) {
for (var i = 0; i < pathParts.length; i++) {
if (pathParts[i] !== "") baseURL += "/" + pathParts[i];
}
}
return baseURL;
}
分割并加入网址:
const s = 'http://free-proxy.cz/en/abc'
console.log(s.split('/').slice(0,3).join('/'))
将其放在标题中,以便在需要时可用。
var base_url = "<?php echo base_url();?>";
您将获得http://localhost:81/your-path-file
或http://localhost/your-path-file
。
文章标签:javascript , jquery
版权声明:本文为原创文章,版权归 javascript 所有,欢迎分享本文,转载请保留出处!
评论已关闭!