ios中safari無痕瀏覽模式下,localStorage的支援情況

來源:互聯網
上載者:User

標籤:fun   存在   cti   前言   無法   手機   nes   代碼   方法   

前言  前階段,測試提了個bug,在蘋果手機中無痕模式下,搜尋按鈕不好使,無法跳頁,同時搜尋曆史也沒有展示(使用者搜尋曆史時使用localStorage儲存)。本文  iOS上Sarfari在無痕模式下,瀏覽器假裝支援localStorage,並在全域window上暴漏了該方法,所以校正是有localStorage對象的,但是調用setItem進行儲存的時候,就報Error: QuotaExceededError ... : The quota has been exceeded. 因此,進行了個小測試......,可能測試不夠全面!1.測試機型  iPhone7 Plus  版本 11.3  iPhone6 Plus  版本 11.3.1  iPhone6     版本 10.2.1  iPhoneSE        版本 9  oppe r92.測試瀏覽器  Sarfari    UC    內建瀏覽器 3.結果:   iOS9,iOS10    Sarfari    setItem 報錯    getItem null   iOS 11    Sarfari    setItem  可set    getItem  可get   iOS 11    UC    setItem  不可set,但不報錯    getItem  null   安卓機    可set,可get 4.結論:  只有iOS9、10還存在無痕瀏覽下localStorage無法使用的情況,iOS11中Sarfari已支援存取localStorage,但是iOS11中UC不支援存localStorage。所以,在開發過程中使用loaclStorage就需要對以上情況進行相容,避免 js 報錯後影響整個頁面的功能。 5.解決代碼及注意要點:  1.使用try/catch  2.只針對setItem,進行判斷“瀏覽器是否開啟無痕模式”
//判斷瀏覽是否支援localStoragefunction isLocalStorageSupport{    try {        var isSupport = ‘localStorage‘ in window && window[‘localStorage‘] !== null;        if (isSupport) {        localStorage.setItem("local_storage_test", "1");        localStorage.removeItem("local_storage_test");        }        return isSupport;    } catch (e) {        return false;    }}//判斷瀏覽器是否開啟無痕模式function  isInPrivate(){      try {        localStorage.setItem("local_storage_test", "1");        localStorage.removeItem("local_storage_test");        return false;      } catch(e){        return true;      }    }

  

ios中safari無痕瀏覽模式下,localStorage的支援情況

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.