html5发表记录保存源码,History.js支持HTML5历史浏览记录

c974120db7a516bcb00883ce00c2244c.png 支持data,titles,replacestate。对于HTML5浏览器,你可以直接输入URL,不再需要哈希来记录。

(function(window,undefined){

// Check Location

if ( document.location.protocol === 'file:' ) {

alert('The HTML5 History API (and thus History.js) do not work on files, please upload it to a server.');

}

// Establish Variables

var

History = window.History, // Note: We are using a capital H instead of a lower h

State = History.getState(),

$log = $('#log');

// Log Initial State

History.log('initial:', State.data, State.title, State.url);

// Bind to State Change

History.Adapter.bind(window,'statechange',function(){ // Note: We are using statechange instead of popstate

// Log the State

var State = History.getState(); // Note: We are using History.getState() instead of event.state

History.log('statechange:', State.data, State.title, State.url);

});

// Prepare Buttons

var

buttons = document.getElementById('buttons'),

scripts = [

'History.pushState({state:1,rand:Math.random()}, "State 1", "?state=1"); // logs {state:1,rand:"some random value"}, "State 1", "?state=1"',

'History.pushState({state:2,rand:Math.random()}, "State 2", "?state=2"); // logs {state:2,rand:"some random value"}, "State 2", "?state=2"',

'History.replaceState({state:3,rand:Math.random()}, "State 3", "?state=3"); // logs {state:3,rand:"some random value"}, "State 3", "?state=3"',

'History.pushState(null, null, "?state=4"); // logs {}, "", "?state=4"',

'History.back(); // logs {state:3}, "State 3", "?state=3"',

'History.back(); // logs {state:1}, "State 1", "?state=1"',

'History.back(); // logs {}, "The page you started at", "?"',

'History.go(2); // logs {state:3}, "State 3", "?state=3"'

],

buttonsHTML = ''

;

// Add Buttons

for ( var i=0,n=scripts.length; i

var _script = scripts[i];

buttonsHTML +=

'

'+_script+'';

}

buttons.innerHTML = buttonsHTML;

})(window);

相关链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值