1// URL LIB 2var urlLib = function() { 3 this.init(); 4}; 5 6urlLib.prototype.init = function() { 7 this.location = window.location; 8 this.url = this.location.href; 9 this.pathname = this.location.pathname; 10 this.search = this.location.search; 11 this.origin = this.location.origin; 12 this.pathArr = this.returnPathArray(); 13 this.queryHesh = this.getQueryStringHesh(); 14}; 15 16urlLib.prototype.removeInitial = function(symbol, str) { 17 return str[0] === symbol ? str.slice(1) : str; 18}; 19 20urlLib.prototype.removeTrailing = function(symbol, str) { 21 return str[str.length - 1] === symbol ? str.slice(0, -1) : str; 22}; 23 24urlLib.prototype.returnPathArray = function() { 25 var pathname = this.pathname; 26 pathname = pathname === '/' ? '' : pathname; 27 28 pathname = this.removeInitial('/', pathname); 29 pathname = this.removeTrailing('/', pathname); 30 pathname = pathname.split('/'); 31 32 return pathname; 33}; 34 35urlLib.prototype.getQueryStringHesh = function() { 36 var queryString = this.removeInitial('?', this.location.search); 37 if ( queryString == '' ) { 38 return {}; 39 } 40 41 var queryStringArr = queryString.split('&'); 42 var queryStringHesh = {}; 43 44 for ( var i = 0; i < queryStringArr.length; i++ ) { 45 var keyVal = queryStringArr[i].split('='); 46 47 queryStringHesh[keyVal[0]] = keyVal[1]; 48 } 49 50 return queryStringHesh; 51}; 52 53urlLib.prototype.setPath = function() { 54 var newPath = '/'; 55 56 for ( var i = 0; i < arguments.length; i++ ) { 57 newPath += arguments[i] + ( i < arguments.length - 1 ? '/' : ''); 58 } 59 60 var newUrl = this.origin + newPath + this.search; 61 62 this.pushState(newUrl); 63 this.init(); 64 65 return this; 66}; 67 68urlLib.prototype.setQuery = function() { 69 if ( arguments.length === 0 ) { 70 return; 71 } 72 73 var oldQueryStringHesh = this.queryHesh; 74 var newQueryStringHesh = {}; 75 76 if ( typeof arguments[0] === 'object' ) { 77 for ( var key in arguments[0] ) { 78 newQueryStringHesh[key] = arguments[0][key]; 79 } 80 } else { 81 var argsLen = arguments.length % 2 != 0 ? arguments.length - 1 : arguments.length; 82 83 for ( var i = 0; i < argsLen; i += 2) { 84 newQueryStringHesh[ arguments[i] ] = arguments[i + 1]; 85 } 86 } 87 88 var combineHeshes = oldQueryStringHesh; 89 90 for ( var key in newQueryStringHesh ) { 91 combineHeshes[key] = newQueryStringHesh[key]; 92 } 93 94 var newQueryString = ''; 95 for ( var key in combineHeshes ) { 96 if ( combineHeshes[key] !== false ) { 97 newQueryString += key + '=' + combineHeshes[key] + '&'; 98 } 99 } 100 101 newQueryString = this.removeTrailing('&', newQueryString); 102 newQueryString = newQueryString.length > 0 ? '?' + newQueryString : newQueryString; 103 104 var newUrl = this.url.replace(this.search, '') + newQueryString; 105 106 this.pushState(newUrl); 107 this.init(); 108 109 return this; 110}; 111 112urlLib.prototype.pushState = function(url) { 113 history.pushState('', '', url); 114}; 115 116urlLib.prototype.reload = function() { 117 this.location.reload(); 118};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.