欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

javascript-hashchange事件和歷史狀態(tài)管理實例分析

 更新時間:2020年04月18日 13:04:25   作者:harmsworth2016  
這篇文章主要介紹了javascript-hashchange事件和歷史狀態(tài)管理,結(jié)合實例形式分析了javascript-hashchange基本功能、原理及歷史狀態(tài)管理相關(guān)操作技巧,需要的朋友可以參考下

本文實例講述了javascript-hashchange事件和歷史狀態(tài)管理。分享給大家供大家參考,具體如下:

hashchange事件

hashchange事件,可以監(jiān)聽URL參數(shù)(#后面的字符串)什么時候發(fā)生變化。

代碼如下

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <script src="EventUtil.js"></script>
  <script>
    EventUtil.addHandler(window, 'load', function () {
      var form = document.forms[0]
      var elements = form.elements
      var mybutton = elements['createBtn']
      var div = document.getElementById('uuid')
      var data = {}
      EventUtil.addHandler(mybutton, 'click', function (event) {
        event = EventUtil.getEvent(event)
        var target = EventUtil.getTarget(event)
        var uuid = getUuid()
        var time = Date.now()
        // 刪除所有子節(jié)點
        while (div.hasChildNodes()) { 
          div.removeChild(div.firstChild); 
        } 
        data[time] = uuid
        div.appendChild(document.createTextNode(uuid))
        window.location.hash = time
      })
      // 監(jiān)聽url的參數(shù)列表(url中#后面的所有字符串)
      EventUtil.addHandler(window, 'hashchange', function (event) {
        event = EventUtil.getEvent(event)
        // 刪除所有子節(jié)點
        while (div.hasChildNodes()) { 
          div.removeChild(div.firstChild); 
        } 
        var text = data[window.location.hash.substring(1)]
        div.appendChild(document.createTextNode(text))
        console.log(`舊url:${event.oldURL}\n新url:${event.newURL}\n當(dāng)前hash:${location.hash}`)
      })
    // 獲取uuid
    function getUuid () {
      return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = Math.random() * 16 | 0
        var v = c === 'x' ? r : (r & 0x3 | 0x8)
        return v.toString(16)
      }).replace(/-/g, '')
    }
  </script>
</head>
<body>
  <form action="" method="get">
    <label for="mybutton">UUID:</label>
    <input id="mybutton" type="button" value="生成" autofocus name="createBtn">
  </form>
  <div id="uuid"></div>
</body>
</html>

歷史狀態(tài)管理

狀態(tài)管理API,能夠在不加載新頁面的情況下改變?yōu)g覽器的URL。使用history.pushState()方法。

代碼如下

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <script src="EventUtil.js"></script>
  <script>
    EventUtil.addHandler(window, 'load', function () {
      var form = document.forms[0]
      var elements = form.elements
      var mybutton = elements['createBtn']
      var div = document.getElementById('uuid')
      var data = {}
      EventUtil.addHandler(mybutton, 'click', function (event) {
        event = EventUtil.getEvent(event)
        var target = EventUtil.getTarget(event)
        var uuid = getUuid()
        var time = Date.now()
        // 刪除所有子節(jié)點
        while (div.hasChildNodes()) { 
          div.removeChild(div.firstChild); 
        } 
        data[time] = uuid
        div.appendChild(document.createTextNode(uuid))
        // 創(chuàng)建新的歷史狀態(tài)
        history.pushState({name: time}, '', 'index.html')
      })
      // 監(jiān)聽瀏覽器“后退”(返回上一頁)事件
      EventUtil.addHandler(window, 'popstate', function (event) {
        event = EventUtil.getEvent(event)
        var state = event.state
        if (state) {
          console.log(state)
        }
      })
    })
    // 獲取uuid
    function getUuid () {
      return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = Math.random() * 16 | 0
        var v = c === 'x' ? r : (r & 0x3 | 0x8)
        return v.toString(16)
      }).replace(/-/g, '')
    }
  </script>
</head>
<body>
  <form action="" method="get">
    <label for="mybutton">UUID:</label>
    <input id="mybutton" type="button" value="生成" autofocus name="createBtn">
  </form>
  <div id="uuid"></div>
</body>
</html>

注意

狀態(tài)管理API只能在服務(wù)端調(diào)用!

感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運行工具http://tools.jb51.net/code/HtmlJsRun測試上述代碼運行效果。

更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript操作DOM技巧總結(jié)》、《JavaScript頁面元素操作技巧總結(jié)》、《JavaScript事件相關(guān)操作與技巧大全》、《JavaScript查找算法技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript錯誤與調(diào)試技巧總結(jié)

希望本文所述對大家JavaScript程序設(shè)計有所幫助。

相關(guān)文章

  • 微信小程序自定義Dialog彈框

    微信小程序自定義Dialog彈框

    這篇文章主要為大家詳細(xì)介紹了微信小程序自定義Dialog彈框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • 微信小程序?qū)崿F(xiàn)錨點跳轉(zhuǎn)

    微信小程序?qū)崿F(xiàn)錨點跳轉(zhuǎn)

    這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)錨點跳轉(zhuǎn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • 微信小程序開發(fā)之map地圖實現(xiàn)教程

    微信小程序開發(fā)之map地圖實現(xiàn)教程

    相信大家現(xiàn)在都知道微信小程序吧,下面這篇文章主要給大家介紹了微信小程序開發(fā)之map地圖的相關(guān)資料,分享出來供大家參考學(xué)習(xí),文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-06-06
  • Javascript中的every()與some()的區(qū)別和應(yīng)用小結(jié)

    Javascript中的every()與some()的區(qū)別和應(yīng)用小結(jié)

    every跟some不同點在于,every要判斷數(shù)組中是否每個元素都滿足條件,只有都滿足條件才返回true,只要有一個不滿足就返回false,這篇文章主要介紹了Javascript中的every()與some()的區(qū)別和應(yīng)用,需要的朋友可以參考下
    2023-05-05
  • JS實現(xiàn)讓網(wǎng)頁背景圖片斜向移動的方法

    JS實現(xiàn)讓網(wǎng)頁背景圖片斜向移動的方法

    這篇文章主要介紹了JS實現(xiàn)讓網(wǎng)頁背景圖片斜向移動的方法,涉及javascript操作背景圖片特效的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-02-02
  • JavaScript forEach 方法跳出循環(huán)的操作方法

    JavaScript forEach 方法跳出循環(huán)的操作方法

    這篇文章主要介紹了JavaScript forEach 方法跳出循環(huán)的操作方法,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2024-01-01
  • Javascript中的匿名函數(shù)與封裝介紹

    Javascript中的匿名函數(shù)與封裝介紹

    這篇文章主要介紹了Javascript中的匿名函數(shù)與封裝介紹,本文分析了jQuery 封裝、Backbone 封裝、Underscore 封裝等內(nèi)容,需要的朋友可以參考下
    2015-03-03
  • javascript 一些用法小結(jié)

    javascript 一些用法小結(jié)

    JavaScript的一些用法總結(jié)
    2009-09-09
  • 解決iframe的frameborder在chrome/ff/ie下的差異

    解決iframe的frameborder在chrome/ff/ie下的差異

    最近的項目中使用了動態(tài)創(chuàng)建iframe的js方法,發(fā)現(xiàn)iframe.frameborder="0"在IE7下不管用,而chrome/ff都正常的,很是郁悶。
    2010-08-08
  • 詳談JS中實現(xiàn)種子隨機(jī)數(shù)及作用

    詳談JS中實現(xiàn)種子隨機(jī)數(shù)及作用

    這篇文章詳細(xì)介紹了種子隨機(jī)數(shù)在JS中如何實現(xiàn)以及有什么作用,希望能幫助有需要的人。下面一起來看看。
    2016-07-07

最新評論