From 9dcfb761503234a18c97a8141a1f96acc56a0012 Mon Sep 17 00:00:00 2001 From: MaiCong Date: Wed, 30 Mar 2016 15:21:35 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A9=E8=B7=AF=E7=94=B1=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E4=BC=A0=E5=8F=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- js/router.js | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/js/router.js b/js/router.js index 9edf158b..80321272 100644 --- a/js/router.js +++ b/js/router.js @@ -99,7 +99,37 @@ */ getUrlFragment: function(url) { var hashIndex = url.indexOf('#'); - return hashIndex === -1 ? '' : url.slice(hashIndex + 1); + if (hashIndex > -1) { + var fragment = url.slice(hashIndex + 1); + if (fragment.indexOf('?') > -1) { + fragment = fragment.slice(0, fragment.indexOf('?')); + } + return fragment; + } + return ''; + }, + /** + * 获取 url 中 hash 后面传入的参数 + * + * 如果没有则返回空对象 + * 如: http://example.com/path/?query=d#123?a=1&b=2 => {a:1, b:2} + * + * @param {String} url url + * @returns {Object} + */ + getUrlSearch: function(url) { + var fragment = url.slice(url.indexOf('#')), + fIndex = fragment.indexOf('?'), + i = 0, fs = [], search = {}; + if (fIndex > -1) { + fragment = fragment.slice(fIndex + 1); + fs = fragment.split('&'); + for (i; i < fs.length; i++) { + var _s = fs[i].split('='); + search[_s[0]] = _s[1]; + } + } + return search; }, /** * 获取一个链接相对于当前页面的绝对地址形式 @@ -140,13 +170,15 @@ toUrlObject: function(url) { var fullUrl = this.getAbsoluteUrl(url), baseUrl = this.getBaseUrl(fullUrl), - fragment = this.getUrlFragment(url); + fragment = this.getUrlFragment(url), + search = this.getUrlSearch(url); return { base: baseUrl, full: fullUrl, original: url, - fragment: fragment + fragment: fragment, + search: search }; }, /** @@ -225,6 +257,8 @@ var $curVisibleSection = $visibleSection.eq(0); var $hashSection; + this.$args = currentUrlObj; + if (currentUrlObj.fragment) { $hashSection = $doc.find('#' + currentUrlObj.fragment); }