如何理解HTML5的HistoryAPI

這篇文章主要講解了“如何理解HTML5的History API”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“如何理解HTML5的History API”吧!

在玉州等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站制作、網(wǎng)站設(shè)計 網(wǎng)站設(shè)計制作定制網(wǎng)站設(shè)計,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),全網(wǎng)營銷推廣,成都外貿(mào)網(wǎng)站制作,玉州網(wǎng)站建設(shè)費(fèi)用合理。

  瀏覽器支持

在寫這篇文章的時候,各主流瀏覽器對History API的支持是非常不錯的,可以點(diǎn)擊此處查看其支持情況,這個鏈接會告訴你支持的瀏覽器,并使用之前,總有良好的實踐來檢測支持的特定功能。

為了用變成方式確定瀏覽器是否支持這個API,可以用下面的一行代碼檢驗:
 

XML/HTML Code復(fù)制內(nèi)容到剪貼板

  1. return !!(window.history && history.pushState);  

此外,我建議參考一下這篇文章:Detect Support for Various HTML5 Features.(ps:后續(xù)會翻譯)

如果你是用的現(xiàn)代瀏覽器,可以用下面的代碼:
 

XML/HTML Code復(fù)制內(nèi)容到剪貼板

  1. if (Modernizr.history) {   

  2.     // History API Supported   

  3. }  

如果你的瀏覽器不支持History API,可以使用history.js代替。
  使用History

HTML 5提供了兩個新方法:

1、history.pushState();                2、history.replaceState();

兩種方法都允許我們添加和更新歷史記錄,它們的工作原理相同并且可以添加數(shù)量相同的參數(shù)。除了方法之外,還有popstate事件。在后文中將介紹怎么使用和什么時候使用popstate事件。

pushState()和replaceState()參數(shù)一樣,參數(shù)說明如下:

1、state:存儲JSON字符串,可以用在popstate事件中。

2、title:現(xiàn)在大多數(shù)瀏覽器不支持或者忽略這個參數(shù),最好用null代替

3、url:任意有效的URL,用于更新瀏覽器的地址欄,并不在乎URL是否已經(jīng)存在地址列表中。更重要的是,它不會重新加載頁面。

兩個方法的主要區(qū)別就是:pushState()是在history棧中添加一個新的條目,replaceState()是替換當(dāng)前的記錄值。如果你還對這個有迷惑,就用一些示例來證明這個區(qū)別。

假設(shè)我們有兩個棧塊,一個標(biāo)記為1,另一個標(biāo)記為2,你有第三個棧塊,標(biāo)記為3。當(dāng)執(zhí)行pushState()時,棧塊3將被添加到已經(jīng)存在的棧中,因此,棧就有3個塊棧了。

同樣的假設(shè)情景下,當(dāng)執(zhí)行replaceState()時,將在塊2的堆棧和放置塊3。所以history的記錄條數(shù)不變,也就是說,pushState()會讓history的數(shù)量加1.

比較結(jié)果如下圖:
如何理解HTML5的History API

到此,為了控制瀏覽器的歷史記錄,我們忽略了pushState()和replaceState()的事件。但是假設(shè)瀏覽器統(tǒng)計了許多的不良記錄,用戶可能會被重定向到這些頁面,或許也不會。在這種情況下,當(dāng)用戶使用瀏覽器的前進(jìn)和后退導(dǎo)航按鈕時就會產(chǎn)生意外的問題。

盡管當(dāng)我們使用pushState()和replaceState()進(jìn)行處理時,期待popstate事件被觸發(fā)。但實際上,情況并不是這樣。相反,當(dāng)你瀏覽會話歷史記錄時,不管你是點(diǎn)擊前進(jìn)或者后退按鈕,還是使用history.go和history.back方法,popstate都會被觸發(fā)。

In WebKit browsers, a popstate event would be triggered after document’s onload event, but Firefox and IE do not have this behavior.(在WebKit瀏覽器中,popstate事件在document的onload事件后觸發(fā),F(xiàn)irefox和IE沒有這種行為)。
Demo示例

HTML:
 

XML/HTML Code復(fù)制內(nèi)容到剪貼板

  1. <div class="container">  

  2.     <div class="row">  

  3.         <ul class="nav navbar-nav">  

  4.             <li><a href="home.html" class="historyAPI">Home</a></li>  

  5.             <li><a href="about.html" class="historyAPI">About</a></li>  

  6.             <li><a href="contact.html" class="historyAPI">Contact</a></li>  

  7.         </ul>  

  8.     </div>  

  9.     <div class="row">  

  10.         <div class="col-md-6">  

  11.             <div class="well">  

  12.                 Click on Links above to see history API usage using <code>pushState</code> method.   

  13.             </div>  

  14.         </div>  

  15.         <div class="row">      

  16.             <div class="jumbotron" id="contentHolder">  

  17.                 <h2>Home!</h2>  

  18.                 <p>Lorem Ipsum is simply dummy text of the <span style="width: auto; height: auto; float: none;" id="5_nwp"><a style="text-decoration: none;" mpid="5" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=f7cb547a6e640f0a&k=printing&k0=printing&kdi0=0&luki=5&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=a0f646e7a54cbf7&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F5360%2Ehtml&urlid=0" id="5_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">printing</span></a></span> and typesetting industry.</p>  

  19.             </div>  

  20.         </div>  

  21.     </div>  

  22. </div>  

JavaScript:

XML/HTML Code復(fù)制內(nèi)容到剪貼板

  1. <script type="text/<span style="width: auto; height: auto; float: none;" id="1_nwp"><a style="text-decoration: none;" mpid="1" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=f7cb547a6e640f0a&k=javascript&k0=javascript&kdi0=0&luki=8&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=a0f646e7a54cbf7&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F5360%2Ehtml&urlid=0" id="1_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">javascript</span></a></span>">  

  2.     jQuery('document').ready(function(){   

  3.              

  4.         jQuery('.historyAPI').on('click', function(e){   

  5.             e.preventDefault();   

  6.             var href = $(this).attr('href');   

  7.                  

  8.             // Getting Content   

  9.             getContent(href, true);   

  10.                  

  11.             jQuery('.historyAPI').removeClass('active');   

  12.             $(this).<span style="width: auto; height: auto; float: none;" id="2_nwp"><a style="text-decoration: none;" mpid="2" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=f7cb547a6e640f0a&k=add&k0=add&kdi0=0&luki=3&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=a0f646e7a54cbf7&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F5360%2Ehtml&urlid=0" id="2_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">add</span></a></span>Class('active');   

  13.         });   

  14.              

  15.     });   

  16.          

  17.     // Adding popstate event listener to handle browser back button    

  18.     window.addEventListener("popstate", function(e) {   

  19.              

  20.         // Get State value using e.state   

  21.         getContent(location.pathname, false);   

  22.     });   

  23.          

  24.     function getContent(url, addEntry) {   

  25.         $.get(url)   

  26.         .done(function( <span style="width: auto; height: auto; float: none;" id="3_nwp"><a style="text-decoration: none;" mpid="3" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=f7cb547a6e640f0a&k=data&k0=data&kdi0=0&luki=6&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=a0f646e7a54cbf7&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F5360%2Ehtml&urlid=0" id="3_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">data</span></a></span> ) {   

  27.                  

  28.             // Updating Content on Page   

  29.             $('#contentHolder').html(data);   

  30.                  

  31.             if(<span style="width: auto; height: auto; float: none;" id="4_nwp"><a style="text-decoration: none;" mpid="4" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=f7cb547a6e640f0a&k=add&k0=add&kdi0=0&luki=3&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=a0f646e7a54cbf7&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F5360%2Ehtml&urlid=0" id="4_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">add</span></a></span>Entry == true) {   

  32.                 // Add History Entry using pushState   

  33.                 history.pushState(null, null, url);   

  34.             }   

  35.                  

  36.         });   

  37.     }   

  38. </script>  

感謝各位的閱讀,以上就是“如何理解HTML5的History API”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對如何理解HTML5的History API這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!

名稱欄目:如何理解HTML5的HistoryAPI
網(wǎng)頁路徑:http://bm7419.com/article40/gihcho.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、網(wǎng)站改版、移動網(wǎng)站建設(shè)自適應(yīng)網(wǎng)站、微信小程序、網(wǎng)站設(shè)計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)