获取地址栏参数小结

以下是各种常用的地址栏部分获取方法
/**
 * 获取与 URL 关联的端口号码
 * @param   * @return  URL端口号码
 */
function getUrlPort(){
    return window.location.port;
}
/**
 * 获取与 URL 的协议部分
 * @param  * @return  URL协议
 */

function getUrlProtocol(){
    return window.location.protocol;
}
/**
 * 获取 href 属性中在井号“#”后面的分段
 * @param  * @return  “#”后面的分段
 */
function getUrlHash(){
    return window.location.hash;
}
/**
 * 获取当前URL参数值
 * @param name
 * @return  参数值
 */
function getUrlParam(name){
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)","i");
    var r = window.location.search.substr(1).match(reg);
    if (r!=null)
        return unescape(r[2]);
    return null;
}
/**
 * 获取 location 或 URL的 port + hostname +  项目名称
 * @param  * @return  port + hostname +  项目名称
 */
function getUrlProject(){
    var filePath = window.location.pathname;
    var filePathIndex = filePath.indexOf('/',1);
    filePath = filePath.substring(0,filePathIndex+1);

    var hostnameUrl = window.location.host;
    return "http://"+hostnameUrl+filePath;
}

猜你喜欢

转载自blog.csdn.net/wh_xmy/article/details/79808331