In a previous article I explored the use of front-end logs under IE, but we know that many people do not use IE as their default browser.
The following is a discussion of log storage and delivery in a high-version Chorme browser.
Because Chorme browser has stopped using plug-ins this year for security issues, it is now not allowed to use plugins in chorme to change the way our local files work. --(Spit a bit, disable the plugin caused my report printing function is not available.) The original layout of the beautiful report can now no longer use chorme, headache)
But since Chorme is a powerful browser, first he gives us some advanced features. such as the local data storage function we are going to use now. We can use it easily in the chorme.
First, it is natural to detect whether the browser supports local storage. In HTML5 , local storage is a window property, including Localstorage and Sessionstorage, from which thename should clearly identify the difference between the two, The former is always local, the latter is only accompanied by the session, the window once closed is gone. Because the log is to be stored for a long time, so we only consider localstorage this scheme, of course, the use of sessionstorage is also possible, but requires you to quickly transfer the log back to the persistence layer, storage. This leads to some logs that cannot be uploaded because of a time problem or no timely mechanism.
Localstorage:
if (window.localstorage) {
Alert (' This browser supports Localstorage ');
}else{
Alert (' This browser does not support Localstorage ');
}
Local storage has multiple methods, the most recommended use is GetItem () and SetItem (), clear key value pairs using removeitem (). If you want to clear all key-value pairs at once, you can use clear ().
We can use a date function to indicate the date of the log, and so on, the following code:
Date.prototype.format = function (format) {
var o = {
"m+": This.getmonth () + 1,//month
"D+": This.getdate (),//day
"H +": this.gethours (),//hour
"m+": This.getminutes (),//minute
"S+": This.getseconds (),//second
"q+": Math.floor ((This.getmonth () + 3)/3),//quarter
"S": This.getmilliseconds ()//millisecond
}
if (/(y+)/.test (format)) format = Format.replace (regexp.$1,
(This.getfullyear () + ""). substr (4-regexp.$1.length));
For (var k in O) if (New RegExp ("(" + K + ")"). Test (format))
Format = Format.replace (regexp.$1,
Regexp.$1.length = = 1? O[K]:
("XX" + o[k]). substr (("" + o[k]). length);
return format;
}
Simple to use:
var storage = Window.localstorage;
var newtime=new Date ();
Storage.setitem (Newtime.format (' yyyy-mm-dd ') + ' username ', 0);
Storage, and then upload the logs on a timed basis and the situation can be.
Front-end log discussion two