js对URL处理方法

2017-01-05 / 0 评论 / 62 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2021年10月27日,已超过905天没有更新,若内容或图片失效,请留言反馈。

封装代码

var urlKit = {
        domain : function(_url){
            return _url?urlKit.pure(_url).replace(/https|http|ftp|rtsp|mms|:|\//g,''):document.domain;
        },
        hostname : function(){
            return location.hostname;
        },
        pathname : function(){
            return location.pathname;
        },
        port : function(){
            return location.port;
        },
        protocol : function(){
            return location.protocol;
        },
        pure : function(_url){
            var match = _url.match(/((https|http|ftp|rtsp|mms):\/\/)?(([0-9a-z_!~*'().&=+$%-]+:)?[0-9a-z_!~*'().&=+$%-]+@)?(([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-z_!~*'()-]+\.)*([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\.[a-z]{2,6})(:[0-9]{1,4})?((\/?)|(\/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+\/?)/g);
            if(match && match.length > 0){
                return match[0];
            }
            return urlKit.domain();
        }
    }

使用

$('a').each(function(i,e){
        if(urlKit.domain()!= urlKit.domain($(e).attr('href')) && $(e).attr('target')!='_blank'){
            $(e).attr('target','_blank');
        }
    });

0

评论 (0)

取消