Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript History.js onpopstate?_Javascript_Html_History.js - Fatal编程技术网

Javascript History.js onpopstate?

Javascript History.js onpopstate?,javascript,html,history.js,Javascript,Html,History.js,在学习使用History API之后,我正在尝试实现History.js。但是我的状态已经不起作用了 History.js中使用了什么作为onpopstate?在历史API中,您将onpopstate与event.state一起使用 我需要知道的是History.js中使用了什么。它是窗口。onstatechange以及它返回什么?基本示例说明: History.Adapter.bind(window,'statechange',function(){ // code }); 和(对于jQue

在学习使用History API之后,我正在尝试实现History.js。但是我的状态已经不起作用了

History.js中使用了什么作为
onpopstate
?在历史API中,您将
onpopstate
event.state
一起使用


我需要知道的是History.js中使用了什么。它是
窗口。onstatechange
以及它返回什么?

基本示例说明:

History.Adapter.bind(window,'statechange',function(){ // code });
和(对于jQuery)使用:


你分不清是推还是砰。这是它的设计。 在推送事件之前,您不应该做任何事情。相反,您应该将所有必需的数据传递给第一个参数

History.pushState(data,title,url)
然后从onstatechange检索数据并执行一些操作


选中此项:

尝试将以下两个事件添加到标记正文中:

  • onpopstate=“(您的代码)”
    -->适用于html5浏览器
  • onhashchange=“(您的代码)”
    -->适用于html4浏览器

  • 如果您遇到任何问题,比如两次运行代码,请尝试构建一个函数,询问
    pushState
    是否准备就绪,并使用
    onpopstate
    。如果没有,则在另一种情况下,使用
    onhashchange

    您应该使用event
    window.statechange
    ,它是由History.js发明的
    window.popstate
    过于通用。您可以通过将事件处理程序绑定到它来对其作出反应,通常如下所示:

    History.Adapter.bind(window, 'statechange', function () {
        var state = History.getState();
        // Your code goes here
    });
    
    或者您可以使用路由器抽象,这应该更容易。我写了一个例子,叫做。一些简单的示例代码:

    var router = new staterouter.Router();
    // Configure routes
    router
      .route('/', getHome)
      .route('/persons', getPersons)
      .route('/persons/:id', getPerson);
    // Perform routing of the current state
    router.perform();
    // Navigate to the page of person 1
    router.navigate('/persons/1');
    

    我也有。

    请注意,每次数据都会被完全序列化,所以不要在数据下包含巨大的对象。你是对的,这是一个真正的麻烦。另外,看看@schellmax指出的问题,你可能想在这个答案上进行扩展,为清楚起见,请提供实际的代码片段。
    var router = new staterouter.Router();
    // Configure routes
    router
      .route('/', getHome)
      .route('/persons', getPersons)
      .route('/persons/:id', getPerson);
    // Perform routing of the current state
    router.perform();
    // Navigate to the page of person 1
    router.navigate('/persons/1');