Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.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
a href=";中的getBaseURL()javascript调用函数&引用;_Javascript_Function - Fatal编程技术网

a href=";中的getBaseURL()javascript调用函数&引用;

a href=";中的getBaseURL()javascript调用函数&引用;,javascript,function,Javascript,Function,我正在通过html页面中的javascript调用基于url,我想添加基本url来调用链接 例如 javascript如下所示: 函数getBaseURL(){ var url=location.href;//包括querystring在内的整个url-也包括:window.location.href; var baseURL=url.substring(0,url.indexOf('/',14)); if(baseURL.indexOf('http://localhost') != -1)

我正在通过html页面中的javascript调用基于url,我想添加基本url来调用链接

例如


javascript如下所示:

函数getBaseURL(){ var url=location.href;//包括querystring在内的整个url-也包括:window.location.href; var baseURL=url.substring(0,url.indexOf('/',14)); if(baseURL.indexOf('http://localhost') != -1) { //本地主机的基本Url var url=location.href;//window.location.href; var pathname=location.pathname;//window.location.pathname; var index1=url.indexOf(路径名); var index2=url.indexOf(“/”,index1+1); var baseLocalUrl=url.substr(0,index2); 返回baseLocalUrl+“/”; } 否则{ //域名的根Url 返回baseURL+“/”; } } 编辑

它只是从有问题的域中获取任何基本URL。另外,我从一个教程中获得了这段代码,因此任何需要较少处理的建议都将非常感谢

第二次编辑

您好,非常感谢您迄今为止的回答,但是,我希望像
中那样调用html标记的函数。这就是问题所在,我不知道怎么做

function getBaseURL () {
   return location.protocol + "//" + location.hostname + 
      (location.port && ":" + location.port) + "/";
}

编辑:解决了Mike Samuel关于非标准端口的观点。

允许使用
(或不使用)的一般解决方案-进一步的改进是使用某种正则表达式从baseURL中删除baseDocument

function getBaseURL () {
    var baseURL = "";
    var baseDocument = "index.html";

    if (document.getElementsByTagName('base').length > 0) {
        baseURL = document.getElementsByTagName('base')[0].href.replace(baseDocument, "");
    } else {
        baseURL = location.protocol + "//" + location.hostname + (location.port && ":" + location.port) + "/";
    }

    return baseURL;
}

我处理的是同一个问题,并不是获取基本URL(如这些答案所示),而是从元素调用javascript函数,然后添加相对路径。使用@Christian Sanchez answer和我找到的其他信息,这是一个使用元素onclick的选项:

JAVASCRIPT:

<script type="text/javascript">
    function getBaseURL(loc) {
        return location.protocol + "//" + location.hostname +
           (location.port && ":" + location.port) + "/" + loc;
    }
</script>

函数getBaseURL(loc){
返回location.protocol+“/”+location.hostname+
(location.port&):“+location.port)+”/“+loc;
}
HTML:



Fiddle在这里:(将鼠标悬停在链接上,可以看到Fiddle中的URL——它显示了iframe的基本URL)

你有问题吗?顺便说一句,
localhost0
localhost.foo.com
是完全有效的域,而不是通常在/etc/hosts或其他名称解析配置中特别处理的域,因此您的
”http://localhost“==baseUrl。子字符串(0,14)
有很多隐藏的假设。您还忽略了
的影响。能否提供一个示例URL以及
baseURL
可以是什么?我觉得你做了很多不必要的处理……如果文档中有一个路径超过一个目录级深度、一个非标准端口或一个
元素,那么这是错误的。@Mike Samuel:我不确定我理解你所说的路径超过一个目录级深度是什么意思,但是
和公共站点的非标准端口很少使用(事实上,我认为我在实践中从未见过
base
标记)。他还表示,他希望“从域问题中获取基本URL”。如果我为库创建此函数,我将考虑所有涉及的细节。但既然我不是,我想我就用Occam的razor,假设他拥有与互联网上95%的站点相同的配置。也许你关于常见环境的断言是正确的,但在帮助论坛上提供简化的代码而不加警告是不好的做法。如果您知道它在某些情况下失败,请在评论中提及。对于每个目录,
http://example.com/foo/bar.html
(在
中没有覆盖)是
http://example.com/foo/
,而不是
http://example.com/
。location.toString()呢。replace(/\/$/,“”)@HorusKol:我的统计数据基于常识和经验。我不打算为即兴估算提供详尽、严格的证据。我不太确定这些问题会导致什么,因为我从未声称我的答案是针对100%的人口的。我采用启发式方法,并为常见情况提供了答案(我使用基于经验的统计数据确定)。如果你认为你能提供一个答案,那就拿出你的时间去做。我很乐意投赞成票。在那之前,我的贡献将不得不做。
<a href="filepath/index.html" onclick="getBaseURL(this.href);">TEST LINK</a>