Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用jquery或javascript获取基本url?_Javascript_Jquery - Fatal编程技术网

如何使用jquery或javascript获取基本url?

如何使用jquery或javascript获取基本url?,javascript,jquery,Javascript,Jquery,在joomla php中,我可以使用$this->baseurl获取基本路径,但我想在jquery中获取基本路径 基本路径可以是以下示例中的任意一个: http://www.example.com/ http://localhost/example http://www.example.com/sub/example 示例也可能会更改。此示例将帮助您 var getUrl = window.location; var baseUrl = getUrl .protocol + "//" + ge

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

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

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

示例也可能会更改。

此示例将帮助您

var getUrl = window.location;
var baseUrl = getUrl .protocol + "//" + getUrl.host + "/" + getUrl.pathname.split('/')[1];
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( '/' );

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

然后,您可以像这样构建url:

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

我在几个Joomla项目中遇到了这种需求。我找到的解决问题的最简单方法是在我的模板中添加一个隐藏的输入字段:

<input type="hidden" id="baseurl" name="baseurl" value="<?php echo $this->baseurl; ?>" />
不像使用纯JavaScript那么花哨,但是简单并且可以完成任务。

这将获得基本url

var baseurl = window.location.origin+window.location.pathname;

把这个放在你的标题里,这样你需要的时候就可以用了

var base_url = "<?php echo base_url();?>";
var base_url=”“;
您将获得
http://localhost:81/your-路径文件
http://localhost/your-path-file

window.location.origin+"/"+window.location.pathname.split('/')[1]+"/"+page+"/"+page+"_list.jsp"

与Jenish的答案几乎相同,但略短。

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

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


我通常通过PHP生成链接。

我建议大家在开发中创建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>
 ...

一些页面标题
var head=document.getElementsByTagName('head')[0];
var base=document.createElement(“base”);
base.href=window.document.location.origin;
头、子(基部);
...
因此,如果您在localhot:8080中,您将从基础访问每个链接或引用的文件,例如:
http://localhost:8080/some/path/file.html
如果您在www.example.com,它将是
http://www.example.com/some/path/file.html

还要注意的是,您所在的每个位置都不应该在hrefs中使用类似glob的引用,例如:父位置原因
http://localhost:8080/
http://localhost:8080/some/path/


请不要将所有超链接引用为完全链接,而不使用bas url。

以防有人希望看到它被分解成一个非常强大的功能

    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;
    }
函数getBaseURL(){ var loc=窗口位置; var baseURL=loc.protocol+“/”+loc.hostname; 如果(loc.port!==“未定义”和&loc.port!==”)baseURL+=“:”+loc.port; //带材/ var pathname=loc.pathname; 如果(pathname.length>0&&pathname.substr(0,1)==“/”)pathname=pathname.substr(1,pathname.length-1); var pathParts=pathname.split(“/”); 如果(pathParts.length>0){ 对于(var i=0;i
简单

$('<img src=>')[0].src
$('')[0].src

生成一个带有空src名称的img,强制浏览器自行计算基本url,无论您是否有
/index.html
或其他内容。

格式为主机名/路径名/搜索

因此,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这里有一些同样适用于
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+"://")]

我只是在同一个阶段,这个解决方案对我有效

在视图中

<?php
    $document = JFactory::getDocument();

    $document->addScriptDeclaration('var base = \''.JURI::base().'\'');
    $document->addScript('components/com_name/js/filter.js');
?>
我不记得我从哪里得到这些信息来给予赞扬,如果我找到了,我会编辑答案

但是您不能说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()将返回

不久前也有同样的问题,我的问题是,我只需要基本url。这里有很多详细的选项,但要解决这个问题,只需使用
窗口。location
对象在浏览器控制台中实际键入该选项,然后按enter键在那里选择选项。对我来说,这很简单:

window.location.origin

在JavaScript中获取基本url的最简单方法

window.location.origin

这是一个非常古老的问题,但以下是我个人使用的方法…

获取标准/基本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.head.innerHTML=document.head.innerHTML+“”;

文档。baseURI
返回与
标记中的值相关的基本URL
P>我惊讶的是,如果设置在<代码> <代码>标签中,没有答案会考虑基本URL。所有当前答案都会尝试获取主机名或服务器名或地址的第一部分。这是一个完整的逻辑,它还考虑了
标记(可能指另一个域或协议):

Jquery格式:

function getBaseURL(){
  if ($("base").length){
     return $("base").attr("href");
  }
  return window.location.origin;
}
在不涉及上述逻辑的情况下,同时考虑
标记和
window.location.origin的速记解决方案:

Js:

Jquery:

var baseURL= $('<a href=".">')[0].href
var baseURL=$(“”)[0]。href
最后一个注释:
document.head.innerHTML = document.head.innerHTML + "<base href='" + url + "' />";
function getBaseURL(){
  var elem=document.getElementsByTagName("base")[0];
  if (typeof(elem) != 'undefined' && elem != null){
     return elem.href;
  }
  return window.location.origin;
}
function getBaseURL(){
  if ($("base").length){
     return $("base").attr("href");
  }
  return window.location.origin;
}
var a=document.createElement("a");
a.href=".";
var baseURL= a.href;
var baseURL= $('<a href=".">')[0].href
const s = 'http://free-proxy.cz/en/abc'
console.log(s.split('/').slice(0,3).join('/'))
var currentUrl = window.location.href;
var originalUrl = window.location.origin;
function getURL() {

var windowurl = window.location.href;
var baseUrl = windowurl.split('://')[1].split('/')[0]; //split function

var xhr = new XMLHttpRequest();
var url='http://'+baseUrl+'/url from controller';
xhr.open("GET", url);
xhr.send(); //object use to send

xhr.onreadystatechange=function() {
    if(xhr.readyState==4 && this.status==200){
 //console.log(xhr.responseText); //the response of the request
        
 document.getElementById("id from where you called the function").innerHTML = xhr.responseText;
}
  }
}