关于使用History.js来管理浏览器状态

时间:2015-12-31 18:10:00

标签: jquery browser-history history.js

CALayer

假设我需要存储页码和排序顺序,那么我需要编写哪些代码?

url部分是强制性的吗?

当用户点击浏览器后退或前进按钮时如何恢复状态?

假设我有一个像这样的对象

History.pushState(data,title,url)

History.pushState({state:1}, "State 1", "?state=1")

所以我可以像这样存储它

var PageState= {
    PageNo:1,
    SortCol:"Name",
    SortOrder:"ASC"
};

当用户点击浏览器后退和前进按钮时,如何使用History.pushState({state:PageState}, null, null); ? 从浏览器历史记录中恢复状态?

寻找代码示例。感谢

1 个答案:

答案 0 :(得分:1)

URL参数是必需的,因为这是附加到地址栏中的基本URL的内容。如果您希望保持相同的路径,但保存新的历史记录状态,则可以使用当前网址:var PageState = { PageNo: 1, SortCol: "Name", SortOrder: "ASC" }; History.pushState(PageState, null, location.href); stateChangeHandler = function(event) { // This PageState is the same object defined above var PageState = event.state; var NewLocation = event.target.location.href; }; // These two statements are nearly equivalent. Only one is needed. window.onpopstate = stateChangeHandler; window.addEventListener("popstate", stateChangeHandler);

当历史状态发生变化时,窗口会发出事件。

function CreateEdge(nodeId1,nodeId2,edgeLabel)
    {

        var curveData = [ { "x": 190,   "y": 100},  { "x": 260,  "y": 50} ];
        var edge = d3.select("svg").append('g');

        //diagonal function that can draw a curve goes in here

        var curve = edge.append("path")
        .attr("d", diagonal)
        .attr("stroke", "#444")
        .attr("stroke-width", 2)
        .attr("fill", "none");

    }

进一步阅读:MDN popstate reference