Use localstorage and js template engine to develop m-site ideas, localstoragejs
Currently, the m-site development method still requests the complete html. The problem is that the amount of data requested each time is too large. Without wifi, the page opening speed is slow, the traffic consumption is also high. Most devices accessing the m station are mobile devices, and their browser versions are high. Therefore, their html5 attribute localstorage supports better, in addition, the m-site page is relatively simple and well structured. If you use localstorage + js parsing template + json data to implement the m structure, the access speed should be greatly improved.
The specific implementation method is as follows:
1. store the html node of the page in localstorage, because the m-site structure is simple, the main page is basically the homepage/channel page/Category page/search result page/product details page/Reservation page, and the page itself is relatively simple, so the overall data volume is not large
2. When accessing the page, the background pushes json data, and then uses the js template parsing tool to assemble and integrate json and localstorage data and fill it in the page
If this method is used, only one json object is returned when the page is accessed.
Advantage: The returned data volume is small, and the page opens a speed block.
Disadvantage: not conducive to seo of pages
Use localStorage to store weather data in JS
LocalStorage is available only in html5. To ensure that the browser supports html5, we recommend that you use Google Chrome.
Create a js file, such as tempcache. js.
// Temporary storage
Var TempCache = {
Cache: function (value ){
LocalStorage. setItem ("EasyWayTempCache", value );
},
GetCache: function (){
Return localStorage. getItem ("EasyWayTempCache ");
},
SetItem: function (key, value ){
LocalStorage. setItem (key, value );
},
GetItem: function (key ){
Return localStorage. getItem (key );
},
RemoveItem: function (key ){
Return localStorage. removeItem (key );
}
};
REFERENCE The js file before calling it.
Set Value
TempCache. setItem ("name", "Zhang San ");
Value
Var name = TempCache. getItem ("name ");
Remove the set value
TempCache. removeItem ("name ");
Hope to help you
Js outputs the value obtained from local localStorage as json (a little difficult)
All localStorage in the ensleep code is spelled as localStroage. When the key does not exist, undefined is output. The final output string of the entire code is not a json object.
Var json ={}, json_sorted ={}, keys = []; for (var k in localStorage) {/^ (? : [1-9] | 1 [0-9]? | 20) $ /. test (k) & (keys. push (k), json ["c" + k] = localStorage [k]);} // json object has been obtained so far // output: Object {c4 = 56, c2 = 236, c1 = 23, c7 = 41} // console. log (json );
// Sort by key below, c1, c2, c3... keys. sort (function (a, B) {return a-B;}); for (var k in keys) {for (var I in json) {I. replace (/c/, "") = keys [k] & (json_sorted [I] = json [I]) ;}// output: object {c1 = 23, c2 = 236, c4 = 56, c7 = 41} // console. log (json_sorted );