-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.mk_location.js
114 lines (91 loc) · 3.16 KB
/
jquery.mk_location.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
;/* $.mk_location
****************************************************************************
-----------------------------------------------------------------------
varsion : 1.1.1
author : http://www.makinokobo.com - oosugi
last update : 2010.11.28 - oosugi
Copyright : Copyright (c) 2010 Skill Partners Inc. All Rights Reserved.
-----------------------------------------------------------------------
$('SELECTOR').mk_current();
=========================================================================
Config
-------------------------------------------------------------------------
****************************************************************************/
mk_location_url = window.location.pathname;
mk_location_root = window.location.protocol+'//'+window.location.hostname;
mk_location_cat_array = mk_location_url.split('/');
mk_location_this_dir = mk_location_url.substr(0,mk_location_url.lastIndexOf('/')+1);
(function($){
$.fn.mk_current = function(config){
config = $.extend({
className: 'current'
},config);
mk_current_className = config.className;
var $a = this.find('a[href!=#]');
$a.each(function(){
var thisHref = $(this).attr('href');
if(navigator.userAgent.indexOf("MSIE") != -1){
thisHref = thisHref.replace(window.location.protocol+'\/\/'+window.location.hostname, '');
};
var targetHref;
if(thisHref.match(/^\/|^http/)){
targetHref = thisHref.replace(mk_location_root,'');
}else{
if(thisHref.match(/\.\.\//)){
var num = 0;
var numTarget = thisHref;
while (thisHref.match(/\.\.\//)){
num++;
thisHref = thisHref.replace(/\.\.\//,'');
}
var thatHref = '';
for(i=0; i<mk_location_cat_array.length-num-1; i++){
thatHref += mk_location_cat_array[i]+'/';
}
targetHref = thatHref+thisHref;
}else{
targetHref = mk_location_this_dir+thisHref;
}
}
if(mk_location_url==targetHref){
$(this).addClass(mk_current_className);
}
});
var $hasCurrent = this.filter(':has(.'+mk_current_className+')');
$hasCurrent.addClass(mk_current_className);
if($hasCurrent.size()==0){
$a.each(function(){
var thisHref = $(this).attr('href');
var targetHref;
if(thisHref.match(/^\/|^http/)){
targetHref = thisHref.replace(mk_location_root,'');
}else{
if(thisHref.match(/\.\.\//)){
var num = 0;
var numTarget = thisHref;
while (thisHref.match(/\.\.\//)){
num++;
thisHref = thisHref.replace(/\.\.\//,'');
}
var thatHref = '';
for(i=0; i<mk_location_cat_array.length-num-1; i++){
thatHref += mk_location_cat_array[i]+'/';
}
targetHref = thatHref+thisHref;
}else{
targetHref = mk_location_this_dir+thisHref;
}
}
targetHref = targetHref.replace(/index\.html|index\.htm|index\.php/,'');
if(!targetHref.match(/\/$/)){
targetHref += '/';
}
if(mk_location_url.match(targetHref)){
$(this).addClass(mk_current_className);
}
});
this.filter(':has(.'+mk_current_className+')').addClass(mk_current_className);
}
return this;
}
})(jQuery);