getBaseURL()javascript调用函数在href =“quot;" [英] getBaseURL() javascript call function in a href=""

查看:554
本文介绍了getBaseURL()javascript调用函数在href =“quot;"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



例如。

< a href =getBaseURL()/ filepath / index.html>页面< / a>

javascript如下:

  function getBaseURL(){
var url = location.href; //包含querystring的整个网址 - 另外:window.location.href;
var baseURL = url.substring(0,url.indexOf('/',14)); (baseURL.indexOf('http:// localhost')!= -1){
//本地主机的基本URL
var url = location.href;

if // 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);

返回baseLocalUrl +/;
}
else {
//根域Url为域名
返回baseURL +/;
}

}



编辑



它只是从有问题的域中获取任何基本网址。另外,我从教程中获得了这段代码,所以任何需要较少处理的建议都会非常好,谢谢。
$ b

SECOND EDIT



大家好,感谢您的回答,但是,我正在寻找调用html标签的函数,如< a href =getURL()/ filepath / file.html>< / a> 。这就是问题出在哪里,我不确定如何做到这一点

解决方法

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

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


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

eg.

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

The javascript is as follows:

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

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

解决方案

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

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

这篇关于getBaseURL()javascript调用函数在href =“quot;&quot;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆