小编meo*_*hmy的帖子

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

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

例如.

<a href="getBaseURL()/filepath/index.html">Page</a>
Run Code Online (Sandbox Code Playgroud)

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 + …
Run Code Online (Sandbox Code Playgroud)

javascript function

15
推荐指数
2
解决办法
4万
查看次数

标签 统计

function ×1

javascript ×1