Skip to main content

js 小片段

· One min read

download data

download('aaa', '1.txt', 'text/plain');

function download(text, name, type) {
var file = new Blob([text], {
type: type
})
var a = $('<a id="download-it">Download it</a>').appendTo('body')
a[0].href = URL.createObjectURL(file)
a[0].download = name
a[0].click()
}

是否当前窗口

var vis = (function () {
var stateKey, eventKey, keys = {
hidden: "visibilitychange",
webkitHidden: "webkitvisibilitychange",
mozHidden: "mozvisibilitychange",
msHidden: "msvisibilitychange"
};
for (stateKey in keys) {
if (stateKey in document) {
eventKey = keys[stateKey];
break;
}
}
return function (c) {
if (c) document.addEventListener(eventKey, c);
return !document[stateKey];
}
})();