-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
1 lines (1 loc) · 1.91 KB
/
index.js
1
var UrlHelper=function(){function t(e){t._init(e)}return t._init=function(t){t||(t=window.location),"string"==typeof t?t.indexOf("?")>t.indexOf("#")&&t.indexOf("#")>-1&&(alert("URL参数格式异常!"),console.error("URL参数格式异常!")):t.href.indexOf("?")>t.href.indexOf("#")&&t.href.indexOf("#")>-1&&(alert("URL参数格式异常!"),console.error("URL参数格式异常!"))},t._isEmptyObject=function(t){for(var e in t)return!1;return!0},t._getParam=function(t){var e,r={},n=/([\s\S]*)=([\s\S]*)/;for(var a in t)null===(e=n.exec(t[a]))?console.info("请检查URL参数"):r[e[1]]=decodeURIComponent(e[2]);return r},t._setParam=function(t){var e="",r=0;for(var n in t)e+=0===r?n+"="+(encodeURIComponent(t[n])||""):"&"+n+"="+(encodeURIComponent(t[n])||""),r++;return e},t.prototype.getSearchParam=function(e){var r;return e||(e=window.location),"object"==typeof e?(r=e.search.substr(1).split("&"),e.search.length?t._getParam(r):{}):e.indexOf("?")>-1?(r=e.split("?")[1].split("#")[0].split("&"),t._getParam(r)):{}},t.prototype.getHashParam=function(e){var r;return e||(e=window.location),"object"==typeof e?(r=e.hash.substr(1).split("&"),e.hash.length?t._getParam(r):{}):e.indexOf("#")>-1?(r=e.split("#")[1].split("&"),t._getParam(r)):{}},t.prototype.setSearchParam=function(e){var r;return r=e&&"object"!=typeof e?"?":t._isEmptyObject(e)?"":"?",r+=t._setParam(e)},t.prototype.setHashParam=function(e){var r;return r=e&&"object"!=typeof e?"#":t._isEmptyObject(e)?"":"#",r+=t._setParam(e)},t.prototype.link=function(t){return t||(t={}),t.path||(t.path=window.location.pathname),t.search||(t.search=""),t.hash||(t.hash=""),""+t.path+("string"==typeof t.search?t.search:this.setSearchParam(t.search))+("string"==typeof t.hash?t.hash:this.setHashParam(t.hash))},t.prototype.jump=function(t){t||(t={}),t.path||(t.path=window.location.pathname),t.search||(t.search=""),t.hash||(t.hash=""),window.location.href=this.link(t)},t}();export default UrlHelper;