濮阳杆衣贸易有限公司

主頁 > 知識(shí)庫 > 深入探究HTML5的History API

深入探究HTML5的History API

熱門標(biāo)簽:邢臺(tái)縣地圖標(biāo)注app 外呼線穩(wěn)定線路 南通數(shù)據(jù)外呼系統(tǒng)推廣 阜陽企業(yè)外呼系統(tǒng) 外呼系統(tǒng)電話怎么投訴 申請(qǐng)400電話流程簡(jiǎn)介 呼和浩特外呼電銷系統(tǒng)排名 pageadm實(shí)現(xiàn)地圖標(biāo)注 地圖標(biāo)注位置能賺錢嗎

History是有趣的,不是嗎?在之前的HTML版本中,我們對(duì)瀏覽歷史記錄的操作非常有限。我們可以來回使用可以使用的方法,但這就是一切我們能做的了。

  但是,利用HTML 5的History API,我們可以更好的控制瀏覽器的歷史記錄了。例如:我們可以添加一條記錄到歷史記錄的列表中,或者在沒有刷新時(shí),可以更新地址欄的URL。
  為什么介紹History API ?

  在這篇文章中,我們將了解HTML 5中History API的來源。在此之前,我們經(jīng)常使用散列值來改變頁面內(nèi)容,特別是那些對(duì)頁面特別重要的內(nèi)容。因?yàn)闆]有刷新,所以對(duì)于單頁面應(yīng)用,改變其URL是不可能的。此外,當(dāng)你改變URL的散列值值,它對(duì)瀏覽器的歷史記錄沒有任何影響。

  然后,現(xiàn)在對(duì)于HTML 5的History API來說,這些都是可以輕易實(shí)現(xiàn)的,但是由于單頁面應(yīng)用沒必要使用散列值,它可能需要額外的開發(fā)腳本。它也允許我們用一種對(duì)SEO友好的方式建立新應(yīng)用。此外,它能減少帶寬,但是該怎么證明呢?

  在文章中,我將用History API開發(fā)一個(gè)單頁應(yīng)用來證明上述的問題。

  這也意味著我必須先在首頁加載必要的資源?,F(xiàn)在開始,頁面僅僅加載需要的內(nèi)容。換句話說,應(yīng)用并不是一開始就加載了全部的內(nèi)容,在請(qǐng)求第二個(gè)應(yīng)用內(nèi)容時(shí),才會(huì)被加載。

  注意,您需要執(zhí)行一些服務(wù)器端編碼只提供部分資源,而不是完整的頁面內(nèi)容。
  瀏覽器支持

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

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

XML/HTML Code復(fù)制內(nèi)容到剪貼板
  1. return !!(window.history && history.pushState);  

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

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

XML/HTML Code復(fù)制內(nèi)容到剪貼板
  1. if (Modernizr.history) {   
  2.     // History API Supported   
  3. }  

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

  HTML 5提供了兩個(gè)新方法:

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

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

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

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

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

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

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

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

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

  比較結(jié)果如下圖:

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

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

  總結(jié)(ps:喜歡這兩個(gè)字~~~^_^~~~)

  HTML 5中的History API 對(duì)Web應(yīng)用有著很大的影響。為了更容易的創(chuàng)建有效率的、對(duì)SEO友好的單頁面應(yīng)用,它移除了對(duì)散列值的依賴。

標(biāo)簽:內(nèi)蒙古 黃山 辛集 蚌埠 德州 楊凌 撫順 鶴崗

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《深入探究HTML5的History API》,本文關(guān)鍵詞  深入,探究,HTML5,的,History,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《深入探究HTML5的History API》相關(guān)的同類信息!
  • 本頁收集關(guān)于深入探究HTML5的History API的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    政和县| 昌宁县| 沁源县| 明光市| 惠州市| 旬邑县| 多伦县| 沙湾县| 英德市| 辰溪县| 蒙山县| 兰坪| 菏泽市| 红河县| 喀喇沁旗| 伊通| 安义县| 四川省| 宾川县| 竹北市| 东城区| 襄城县| 霞浦县| 福鼎市| 新昌县| 泰州市| 鄂尔多斯市| 白沙| 酒泉市| 息烽县| 婺源县| 罗甸县| 新津县| 四平市| 岚皋县| 溧水县| 织金县| 高尔夫| 扎兰屯市| 普宁市| 田东县|