【问题标题】:History.js bookmark support for HTML5 (pushState) and HTML4 (#)History.js 书签支持 HTML5 (pushState) 和 HTML4 (#)
【发布时间】:2012-11-20 16:34:15
【问题描述】:

我在 HTML5 和 HTML4 浏览器中都有通过 History.js 工作的后退和前进按钮。

我现在尝试通过解析 url 并加载查询字符串中的内容来添加书签支持。我通常用来执行此操作的代码是:

var urlParams = {};
            (function () {
            var match,
                pl     = /\+/g,  
                search = /([^&=]+)=?([^&]*)/g,
                decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
                query  = window.location.search.substring(1);

            while (match = search.exec(query))
            urlParams[decode(match[1])] = decode(match[2]);
            })();  

这适用于 HTML5,但显然不适用于 HTML4。什么是解析 HTML4 中的哈希和 HTML5 中的子字符串的好方法?

【问题讨论】:

    标签: hash history.js url-parsing


    【解决方案1】:

    在此页面上找到了解决方案:https://getsatisfaction.com/balupton/topics/history_js_and_retrieving_state_from_a_url

    “但是,您可以从我的一个名为 jQuery Sparkle 的实用程序项目中使用此功能来满足您的需求:https://github.com/balupton/jquery-sparkle/blob/master/scripts/resources/core.string.js#L164

    所以你会这样做:

    var State = History.getState(), dataQuery = State.url. queryStringToJSON();"
    

    【讨论】:

      猜你喜欢
      • 2012-02-04
      • 2013-10-19
      • 2011-09-18
      • 1970-01-01
      • 2011-10-08
      • 2012-06-19
      • 2023-03-09
      • 1970-01-01
      • 2015-10-14
      相关资源
      最近更新 更多