封装代码
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)