»ùÓÚJquery.history½â¾öajaxµÄÇ°½øºóÍËÎÊÌ⣬jquery.historyajax
ÒÔÏÂÄÚÈÝÊǹØÓÚJquery.history½â¾öajaxµÄÇ°½øºóÍËÎÊÌ⣬¾ßÌåÏêÇéÇë¿´ÏÂÎÄ¡£
±¾ÎĵÄÇ°ÌáÊÇ»ùÓÚºǫ́µÄ£¬ËùÒÔÕâÀï²»»á¿¼ÂÇseoµÄÎÊÌ⡣ͬʱ£¬»ùÓÚºǫ́µÄ¹ÜÀíϵͳ£¬Ò²²»ÐèÒª±»Êղأ¬ËùÒÔÒ²²»»á¿¼ÂÇˢеÄÕâÖÖÀàËÆÖ±½ÓÇÃÍøÖ·µÄÇé¿ö£¡£¡£¡
ÕâÀïʹÓõÄÊÇhtml5ÖеÄhistory.state À´½â¾ö¡£
ÍøÉÏÒѾÓÐÁËjsµÄ¿ªÔ´½â¾ö·½°¸pushState¡£Ïê¼ûpjax
µ«ÊÇÕâ¸ö·½°¸²»ÊʺÏÎÒµÄÏîÄ¿£¨ºǫ́ÏîÄ¿£©£¬ÓÈÆäÊÇÖ÷ÒªµÄ·½·¨Óе㲻¹»Óá£
ÎÒʹÓõÄÊÇ jquery.history.js ¿ÉÒԲο¼Õâ¸ö history.js
¿´ÏÂÃæµÄ´úÂë
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script> <script src="js/jquery.history.js"></script> </head> <body>̞ <input type="text"/> <br/>
Óû§id <input type="text"/> <br/>
ÏÔʾ½á¹û
<div><input type="text"/></div> <br/> <a funname="displayinfo" title="wxq" href="javascript:void(0);">Ìá½»</a> <br/> log: <textarea></textarea> </body> </html> <script> ///Ëæ»úÊý function guid() { function S4() { return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1); } return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4()); } /// »Øµ÷µÄ·½·¨ function doCallback(fn, args) { fn.apply(this, args); } ///»ñÈ¡²éѯ×Ö·û´® function GetQueryString() { var result = ""; var url = location.href; if(url.indexOf("?")>0){ result = url.substring(url.indexOf("?") + 1, url.length); if (result != "") { result = result + "&"; } } return "?" + result; } // ÓÃÀ´±£´æµØÖ·À¸µÄ²ÎÊý£¬µÚÒ»´ÎÒ³Ãæ¼ÓÔصÄʱºòÖ´ÐС£ ÓÃÀ´Ð޸ĵØÖ·À¸µÄʱºò£¬±£ÁôÔʼµÄ²ÎÊý var globalQuerystr; var golbalState = { "globalPara": {},// jsµ÷Ó÷½·¨Ö´ÐÐʱÓõÄÈ«¾Öjs²ÎÊý£¬ "globalControlValue": {},//jsÐèÒªÐÞ¸ÄvalueµÄhtml×é¼þ ÈçÎı¾ ÀàËÆ$(¡°#xxx¡±).val() "globalControlHtml": {},// jsÐèÒªÐÞ¸ÄÄÚ²¿htmlÄÚÈݵÄhtml×é¼þ ÀàËÆ $(¡°#yy¡±).html() "CallbackFunctions": "" // »Øµ÷µÄjs·½·¨ ¿ÉÒÔ×Ô¼ºÊµÏÖ £¬ÓöººÅ·Ö¸ô£¬Ò»°ãÊÇÒ»¸ö·½·¨Ãû }; ///È«¾Ö±äÁ¿µÄÎÊÌâ globaPara ±£´æÈ«¾Ö±äÁ¿ ///¶à¸ö¿Ø¼þ±ä»¯ µ«ÊDz»ÔÚÒ»¸öÈÝÆ÷ÄÚµÄÎÊÌâ Êý×éµÄ·½Ê½ History.pushState({state:1,rand:Math.random(), "globaPara":globaPara, "valuecontrol":[{"txtuser":"1"},{"txtname":"2"}] "htmlcontrol":[{"htmltxt":"<b>hello</b>"},{"htmlcheckbox1":""}] },"title","?data=1" //¿Ø¼þÀàÐ͵ÄÎÊÌâ Óеı£´æÖµ¾ÍÐÐ Óеı£´æhtml¸ü·½±ã (function (window, undefined) { globalQuerystr = GetQueryString(); var History = window.History, // Note: We are using a capital H instead of a lower h State = History.getState(), $log = $('#log'); console.info("Ò³Ãæ¼ÓÔØÍê±Ï"); History.log('initial:', State.data, State.title, State.url); //Ò³ÃæµÚÒ»´Î¼ÓÔصÄʱºò StateûÓÐÊôÐÔ 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 golbalState = State.data; if (golbalState) { //Ð޸ĿؼþµÄvalÖµ if (golbalState.globalControlValue) { for (var i = 0; i < golbalState.globalControlValue.length; i++) { $("#" + golbalState.globalControlValue[i].controlid).val(golbalState.globalControlValue[i].controlvalue); } } //ÐÞ¸ÄÈ«¾Ö±äÁ¿ if (golbalState.globalPara) { for (var i = 0; i < golbalState.globalPara.length; i++) { eval("" + golbalState.globalPara[i].globalname + " =" + golbalState.globalPara[i].globalvalue + ";"); } } //Ð޸ĿؼþµÄhtml if (golbalState.globalControlHtml) { for (var i = 0; i < golbalState.globalControlHtml.length; i++) { $("#" + golbalState.globalControlHtml[i].controlid).html(golbalState.globalControlHtml[i].controlhtml); } } //Ö´Ðлָ´²ÎÊýºóÒªÖ´Ðеĺ¯Êý if (golbalState.CallbackFunctions != "") { doCallback(eval("" + golbalState.CallbackFunctions), null); } //Çå¿Õº¯Êý golbalState.CallbackFunctions = ""; } History.log('statechange:', State.data, State.title, State.url); }); })(window); function displayinfo() { console.info("index:" + History.getCurrentIndex); //ajax·½·¨£¬´Ó·þÎñÆ÷»ñÈ¡Êý¾Ý $("#txtinfo").val("ÎÒ°®" + $("#txtUser").val() + $("#txtId").val()); } $(function () { $(".apjax").on("click", function () { golbalState.globalControlValue = [{"controlid": "txtUser", "controlvalue": $("#txtUser").val()}]; golbalState.CallbackFunctions = $(this).attr("funname"); History.pushState(golbalState, $(this).attr("title"), globalQuerystr + "rnd=" + guid()); }); }) </script>Ò» ÏÈÒýÓÃjquery ºÍhistory.js µÄjs
¶þ
History.Adapter.bind(window, 'statechange', function () { ±íʾÔÚµã»÷ºóÍË Ç°½ø»òhistoryµÄjs·½·¨Ê±£¬¼à¿Ø±ä»¯¡£µ±±ä»¯Ê±Ö´ÐÐ×Ô¶¨ÒåµÄ·½·¨¡£
History.pushState ÓÐÈý¸ö²ÎÊý state title ×Ô¶¨ÒåµÄµØÖ·²éѯ²ÎÊý¡£
¸Ã·½·¨ÍùÀúÊ·¼Ç¼ÀïÌí¼ÓÊý¾Ý£¬Ð޸ĵØÖ·À¸
History.getState() »ñÈ¡µ±Ç°µØÖ·µÄstate²ÎÊý
Èý Á÷³Ì
1 Ê×ÏÈ»ñÈ¡µØÖ·À¸µÄ²ÎÊý²¢±£´æ
¡¡