常用javascript代码整理

来源:互联网 发布:刷直通车淘宝抓吗 编辑:程序博客网 时间:2024/05/17 02:59
//回到顶部    function goTop() {        if (document.body.scrollTop) { //非标准写法,chrome能识别            var d = document.body;        } else { //标准写法            var d = document.documentElement;        }        var timer = setInterval(function () {            d.scrollTop -= Math.ceil(d.scrollTop * 0.1);            if (d.scrollTop == 0)                clearInterval(timer);        }, 10);    }    (function(){    var bp = document.createElement('script');    bp.src = '//push.zhanzhang.baidu.com/push.js';    var s = document.getElementsByTagName("script")[0];    s.parentNode.insertBefore(bp, s);   })();   function setCookie(name, value) {    var times= arguments[2]/(24*60)|| 30;    var exp = new Date();    exp.setTime(exp.getTime() + times * 24 * 60 * 60 * 1000);    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();}/** * 取到cookie * @param {string} name * @returns {string} value */function getCookie(name) {    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");    if (arr = document.cookie.match(reg))        return unescape(arr[2]);    else        return null;}Date.prototype.format = function(format) {    var date = {        "M+": this.getMonth() + 1,        "d+": this.getDate(),        "h+": this.getHours(),        "m+": this.getMinutes(),        "s+": this.getSeconds(),        "q+": Math.floor((this.getMonth() + 3) / 3),        "S+": this.getMilliseconds()    };    if (/(y+)/i.test(format)) {        format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));    }    for (var k in date) {        if (new RegExp("(" + k + ")").test(format)) {            format = format.replace(RegExp.$1, RegExp.$1.length == 1                ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));        }    }    return format;Date.prototype.format = function(format) {    var date = {        "M+": this.getMonth() + 1,        "d+": this.getDate(),        "h+": this.getHours(),        "m+": this.getMinutes(),        "s+": this.getSeconds(),        "q+": Math.floor((this.getMonth() + 3) / 3),        "S+": this.getMilliseconds()    };    if (/(y+)/i.test(format)) {        format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));    }    for (var k in date) {        if (new RegExp("(" + k + ")").test(format)) {            format = format.replace(RegExp.$1, RegExp.$1.length == 1                ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));        }    }    return format;} function addBookmark() {        var title = '';        var url = '';        var ua = navigator.userAgent.toLowerCase();        if (ua.indexOf("360se") > -1) {            alert("由于360浏览器功能限制,请按 Ctrl+D 手动收藏!");        } else if (ua.indexOf("msie 8") > -1) {            window.external.AddToFavoritesBar(url, title); //IE8        } else if (document.all) {            try {                window.external.addFavorite(url, title);            } catch (e) {                alert('您的浏览器不支持,请按 Ctrl+D 手动收藏!');            }        } else if (window.sidebar) {            window.sidebar.addPanel(title, url, "");        } else {            alert('您的浏览器不支持,请按 Ctrl+D 手动收藏!');        }    }

0 0
原创粉丝点击