ÕâϵÁÐÎÄÕÂÖ÷ÒªÊÇѧϰHtml5Ïà¹ØµÄ֪ʶµã£¬ÒÔѧϰAPI֪ʶµãΪÈë¿Ú£¬ÓÉdzÈëÉîµÄÒýÈëʵÀý£¬Èôó¼ÒÒ»²½Ò»²½µÄÌå»á"h5"Äܹ»×öʲô£¬ÒÔ¼°ÔÚʵ¼ÊÏîÄ¿ÖÐÈçºÎÈ¥ºÏÀíµÄÔËÓôﵽʹÓÃ×ÔÈ磬ÍêÃÀ¼ÝÔ¦O(∩_∩)O~
ÏȳöÒ»µÀÌâÄ¿£ºÒÔϲ»ÊÇHTML5ÐÂÔöµÄAPIÊÇ
Ò»£¬¿ªÆª·ÖÎö
ºÃÁË£¬·Ï»°²»¶à˵£¬Ö±½Ó½øÈë½ñÌìµÄÖ÷Ì⣬½ñÌìÖ÷Òª½²µÄÊÇ“History API”¼°ÔÚµ¥Ò³Ó¦ÓÃÖеÄ×÷Ó㬲¢ÇÒ»áÒýÈëÒ»¸öʵ¼ÊµÄÀý×Ó×öΪ½²½âµÄÔÐÍ·¶Àý£¬ÏÈÀ´¿´¿´“History API”£º
¡¡¡¡ÎªÁËÌá¸ßWebÒ³ÃæµÄÏìÓ¦Ëٶȣ¬Ô½À´Ô½¶àµÄ¿ª·¢Õß¿ªÊ¼²ÉÓõ¥Ò³Ãæ½á¹¹£¨single-page application£©µÄ½â¾ö·½°¸¡£ËùνµÄµ¥Ò³Ãæ½á¹¹¾ÍÊÇÖ¸¶à¸öÒ³Ãæ¼äÇл»Ê±£¬²»Ë¢Ðµ±Ç°Õû¸öÒ³Ã棬¸üÐÂÒ³ÃæչʾÊý¾Ý£¬²¢ÇÒÏàÓ¦µØ¸Ä±äµØÖ·À¸ÖеÄurl£¬ÒÔʹÓû§¿ÉÒÔ·ÖÏíÕâ¸öurl¡£
¡¡¡¡Èç¹ûÄãʹÓÃchrome»òÕßfirefoxµÈä¯ÀÀÆ÷·ÃÎÊ"github.com¡¢plus.google.com"µÈÍøվʱ£¬Ï¸ÐĵÄÄã»á·¢ÏÖÒ³ÃæÖ®¼äµÄµã»÷ÊÇͨ¹ýajaxÒì²½ÇëÇóµÄ£¬
ͬʱҳÃæµÄURL·¢ÉúÁËÁ˸ı䡣²¢ÇÒÄܹ»ºÜºÃµÄÖ§³Öä¯ÀÀÆ÷Ç°½øºÍºóÍË¡£ÊÇʲôÓÐÕâôǿ´óµÄ¹¦ÄÜÄØ£¿¶÷£¬Õâ¾Í»á˵µ½½ñÌìµÄÖ÷½Ç£¬HTML5ÀïÒýÓÃÁËеÄAPI£º
“history.pushState”ºÍ“history.replaceState”£¬¾ÍÊÇͨ¹ýÕâ¸ö½Ó¿Ú×öµ½ÎÞˢиıäÒ³ÃæURLµÄ¡£ÏÈÀ´¿´¿´"history"½Ó¿ÚµÄÏêϸ·½·¨£º
interface History { readonly attribute long length; readonly attribute any state; void go(optional long delta); void back(); void forward(); void pushState(any data, DOMString title, optional DOMString? url = null); void replaceState(any data, DOMString title, optional DOMString? url = null); };
£¨¶þ£©£¬ÖصãAPI˵Ã÷
ÔÚÕâÀï˵Ã÷Ò»µã£º"window.history.replaceState"ºÍ"window.history.pushState"ÀàËÆ£¬²»Í¬Ö®´¦ÔÚÓÚreplaceState²»»áÔÚwindow.historyÀïÐÂÔöÀúÊ·¼Ç¼µã£¬ÆäЧ¹ûÀàËÆÓÚwindow.location.replace(url)£¬¶¼ÊDz»»áÔÚÀúÊ·¼Ç¼µãÀïÐÂÔöÒ»¸ö¼Ç¼µãµÄ¡£µ±ÄãΪÁËÏìÓ¦Óû§µÄijЩ²Ù×÷£¬¶øÒª¸üе±Ç°ÀúÊ·¼Ç¼ÌõÄ¿µÄ״̬¶ÔÏó»òURLʱ£¬Ê¹ÓÃreplaceState()·½·¨»áÌرðºÏÊÊ¡£
£¨Èý£©£¬ÒýÈëʵÀý
¡¡¡¡½ñÌì˵˵µ¥Ò³Ó¦ÓÃÖÐÎÒÃÇͨ³£»áÕâÑù×ö£¬ÓÐÒ»¸ö²Ëµ¥ÁÐ±í£¬µã»÷Ïà¹Ø²Ëµ¥ÏîÈ»ºó¶¯Ì¬¼ÓÔØÏà¹ØÄ£¿é£¬ËùÓз½Ê½¶¼ÊÇ»ùÓÚÒì²½ÇëÇóµÄ£¬ÃÀÖв»×ãÊÇ£¬ÎÒÃǻᷢÏÖµØÖ·À¸²»»áÓÐÈκα仯£¬ÒÔ¼°ä¯ÀÀÆ÷ÖеÄÇ°½øºÍºóÍ˲Ù×÷Ò²²»»áÓÐÈκÎÏìÓ¦£¬Õâ¶ÔÓû§²»ÊǺÜÓѺã¬ËùÒÔΪÁ˽â¾öÕâ¸öÎÊÌâ“History”¾ÍÓÐÓÃÎäÖ®µØÁË£¬ÄÇôÈçºÎ×öµ½µÄÄÇ£¿²»¼±ÏÈ¿´Ò»ÏÂÀý×ÓÖеÄЧ¹ûͼչʾ£¬È»ºóÒ»²½²½·ÖÎö,ÈçÏÂËùʾ£º
ÒÔÏÂÊǼà²âÊý¾Ý£¬ÏàͬµÄurlÔÙ´Îˢв»»áÖظ´ÇëÇó¡£
ÎÒÃÇÊáÀíÒ»ÏÂÁ÷³Ì£º
¡¡¡¡¡¡¡¡Ò³ÃæÊ×´ÎÔØÈ룬ËäÈ»ÎÒÃÇ·ÃÎʵÄURLÊÇ"http://localhost:8888/bbSPA.html"£¬µ«ÊÇ£¬Êµ¼ÊURLÈ·ÊÇ£º
"http://localhost:8888/bbSPA.html#shanghai"£¬”history.replaceState“Íê³ÉÁ˳õʼ»¯urlÇл»¹¤×÷£¬²¢ÇÒ³õʼ×öÁ˼ÓÔØ
"shanghai.data"µÄÊý¾Ý¹¤×÷£¬Êó±êµã»÷×ó±ßµÄÈÎÒâÒ»¸ö²Ëµ¥ÏÓÒ²àÄÚÈÝÊÇAjaxÔØÈ룬²¢ÇÒÒ³ÃæµÄURLËæÖ®·¢Éú¸Ä±ä£¬ÀýÈ磬µã»÷±±¾©¡£
´Ëʱ£¬ÎÒÃǵã»÷µØÖ·À¸µÄºóÍË°´Å¥£¬Óֻص½ÁËÉϺ££¬²¢ÇÒÏÔʾÄÚÈÝ¡£ÔÀíºÜ¼òµ¥£¬¾ÍÊÇͨ¹ý¼àÌý”window.onpopstate“£¬´ïµ½ÁË×ÔÓÉÇл»µÄ×÷Óá£
ºÃÁË£¡ÆäʵºÜ¼òµ¥´ó¼ÒÒ²³¢ÊÔ×Ô¼ºÊµ¼ùһϣ¬ÒÔÏÂÊÇÍêÕû´úÂ룺
¡¡¡¡£¨1£©£¬html²¿·Ö´úÂë¡¡¡¡
bbSPA²âÊÔÒ³Ãæ_ºÎÎÊÆðul id="list" style="float:left; list-style:none;" ±±¾©ÉϺ£ÉîÛÚ¹ãÖÝÌì½òdiv id="content-main" style="margin-left:50px; float:left; width:220px; border:1px solid #ccc; height:120px; color:#ff3300;"
£¨2£©£¬Js²¿·Ö´úÂë
$(function(){ _init() ; }) ; _init(){ var root = $("#list") ; var defaultHash = root.find("li a").eq(1).attr("href") ; var currentHash = window.location.hash ; _addToHistory(defaultHash,true) ; if(currentHash && currentHash != defaultHash){ _showContent((currentHash.split("#")[1])) ; } else{ _showContent((defaultHash.split("#")[1])) ; } $("#list").on("click","a",function(e){ var action = ($(this).attr("href").split("#")[1]) ; _showContent(action) ; e.preventDefault() ; }) ; window.addEventListener("popstate",function(e){ if(e.state && e.state.hash){ var hash = e.state.hash ; if(_history[1] && hash === _history[1].hash){//´æÔÚÀúÊ·¼Ç¼£¬Ö¤Ã÷ÊǺóÍËʼþ _showContent(hash.split("#")[1].toLowerCase()) ; } ; } } else{ return ; } },false) ; } ; function _showContent(action){ var samePage = _history[0]["hash"] == "#" + action ; ; } _loadContent(action + ".data").done(function(data){ _renderContent(data["content"]) ; _addToHistory("#" + action,samePage) ; }).fail(function(){ throw new Error("load content error !") ; }) ; } ; function _loadContent(url){ return $.ajax({ url : url , dataType : "json" }) ; } ; function _renderContent(text){ $("#content-main").text(text) ; } ; function _addToHistory(hash,noState){ var obj = { hash : hash } ; if(noState){ _history.shift(obj) ; window.history.replaceState(obj,"",hash) ; } else{ window.history.pushState(obj,"",hash) ; } _history.unshift(obj) ; } ;
£¨ËÄ£©£¬×îºó×ܽá
¡¡¡¡£¨1£©£¬Àí½âHistory ApiµÄʹÓ÷½Ê½ÒÔ¼°¾ßÌåʵÀýÖÐʹÓõÄÄ¿µÄÊÇΪÁ˽â¾öÄÄЩÎÊÌâ¡£
¡¡¡¡£¨2£©£¬Á½¸öºËÐÄApiµÄ²»Í¬Ö®´¦ÔÚÄÄ¡£
¡¡¡¡£¨3£©£¬²âÊÔ±¾Àý×ÓµÄ×¢ÒâÊÂÏîÈçÏ¡£
¡¡¡¡²âÊÔÐèÒª´î½¨Ò»¸öweb·þÎñÆ÷£¬ÒÔµÄÐÎʽȥ·ÃÎʲÅÄÜÉúЧ£¬Èç¹ûÄãÔÚ±¾µØ²âÊÔÒÔfile://ÕâÑùµÄ·½Ê½ÔÚä¯ÀÀÆ÷´ò¿ª£¬¾Í»á³öÏÖÈçϵÄÎÊÌ⣺
Uncaught SecurityError: A history state object with URL 'file:///C:/xxx/xxx/xxx/xxx.html' cannot be created in a document with origin 'null'.
¡¡ÒòΪÄãÒªpushStateµÄurlÓ뵱ǰҳÃæµÄurl±ØÐëÊÇͬԴµÄ£¬¶øfile://ÐÎʽ´ò¿ªµÄÒ³ÃæÊÇûÓÐoriginµÄ£¬ËùÒԻᱨÕâ¸ö´íÎó¡£
ÒÔÉϾÍÊDZ¾ÎĵÄÈ«²¿ÄÚÈÝÁË£¬Ï£Íû´ó¼ÒÄܹ»Ï²»¶¡£
ÍƼö£º
¡¡