在学习使用History API之后,我正在尝试实现History.js。然而,我的popstate不再工作了。
在History.js中用作onpopstate的是什么?在历史API中,您可以在event.state中使用onpopstate。
我需要知道的是History.js中使用了什么。它是window.onstatechange吗?它返回什么?
发布于 2012-02-18 14:04:07
你不能说它是推送的还是流行的。这是它的设计。在推送事件之前,不应该做任何事情。相反,您应该将所有必需的数据传递给第一个参数。
History.pushState(data,title,url)然后从onstatechange中检索数据并执行一些操作。
发布于 2011-11-25 21:20:26
基本示例如下:
History.Adapter.bind(window,'statechange',function(){ // code });而this gist (用于jQuery)使用:
$(window).bind('statechange',function(){ // code });发布于 2013-01-03 04:40:44
你应该使用History.js发明的event window.statechange,因为window.popstate太通用了。您可以通过将事件处理程序绑定到它来响应它,通常如下所示:
History.Adapter.bind(window, 'statechange', function () {
var state = History.getState();
// Your code goes here
});或者,您可以使用路由器抽象,这应该更容易。例如,我编写了一个名为StateRouter.js的代码。一些简单的示例代码:
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');我也有a fiddle for demonstration purposes。
https://stackoverflow.com/questions/7986601
复制相似问题