在 href="" 中的 getBaseURL() javascript 调用函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6179882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:36:00  来源:igfitidea点击:

getBaseURL() javascript call function in a href=""

javascriptfunction

提问by meohmy

I am calling the based url through javascript in a html page and I want to add the base url to call a link

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

eg.

例如。

<a href="getBaseURL()/filepath/index.html">Page</a>

The javascript is as follows:

javascript如下:

function getBaseURL() {
    var url = location.href;  // entire url including querystring - also: window.location.href;
    var baseURL = url.substring(0, url.indexOf('/', 14));

    if (baseURL.indexOf('http://localhost') != -1) {
        // Base Url for localhost
        var url = location.href;  // window.location.href;
        var pathname = location.pathname;  // window.location.pathname;
        var index1 = url.indexOf(pathname);
        var index2 = url.indexOf("/", index1 + 1);
        var baseLocalUrl = url.substr(0, index2);

        return baseLocalUrl + "/";
    }
    else {
        // Root Url for domain name
        return baseURL + "/";
    }

}

EDIT

编辑

Its just to get any base URL from the domain in question. Also, I got this code from a tutorial so any suggestions that will require less processing would be great thanks

它只是从相关域中获取任何基本 URL。另外,我从教程中得到了这段代码,所以任何需要较少处理的建议都会非常感谢

SECOND EDIT

第二次编辑

Hi All thanks for your answers so far, however, I am looking to call the function to the html tag as in <a href="getURL()/filepath/file.html></a>. This is where the problem lies, I am unsure how to do that

大家好,感谢您到目前为止的回答,但是,我希望将该函数调用到 html 标记中,如<a href="getURL()/filepath/file.html></a>. 这就是问题所在,我不确定该怎么做

回答by Cristian Sanchez

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

Edit: Addressed Mike Samuel's point on nonstandard ports.

编辑:解决了迈克塞缪尔关于非标准端口的观点。

回答by HorusKol

A general solution to allow for the use of <base>(or not) - a further improvement would be to have the baseDocument removed from the baseURL using some kind of regex.

允许使用<base>(或不使用)的通用解决方案- 进一步的改进是使用某种正则表达式从 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;
}

回答by deebs

I am dealing with the same issue, and it isn't so much getting the base URL (as these answers have shown), but calling that javascript function from the element and then adding a relative path. Using @Christian Sanchez answer and other info I've found, this is an option using the onclick of the element:

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

JAVASCRIPT:

爪哇脚本:

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

HTML:

HTML:

<a href="filepath/index.html" onclick="getBaseURL(this.href);">TEST LINK</a>

Fiddle is here: http://jsfiddle.net/62g2bkyh/(hover over link to see URL in fiddle - which shows base URL of iframe).

小提琴在这里:http: //jsfiddle.net/62g2bkyh/(将鼠标悬停在链接上以查看小提琴中的 URL - 显示 iframe 的基本 URL)。