常用的js代码片段

获取选择到的字

function getSelectedText() {
	if (window.getSelection) { // This technique is the most likely to be standardized.         
		// getSelection() returns a Selection object, which we do not document.        
		return window.getSelection()
			.toString();
	} else if (document.getSelection) {
		// This is an older, simpler technique that returns a string        
		return document.getSelection();
	} else if (document.selection) {
		// This is the IE-specific technique.         
		// We do not document the IE selection property or TextRange objects.         
		return document.selection.createRange()
			.text;
	}
}

阻止右键

document.body.onselectstart = document.body.oncontextmenu = function() {
	return false;
}

动态加载

//NO.1
function loadScript(url, callback){
    var script = document.createElement ("script")
    script.type = "text/javascript";
    if (script.readyState){ //IE
        script.onreadystatechange = function(){
            if (this.readyState == "loaded" || this.readyState == "complete"){
                this.onreadystatechange = null;
                callback();
            }
        };
    } else { //Others
        script.onload = function(){
            callback();
        };
    }
    script.src = url;
    document.getElementsByTagName("head")[0].appendChild(script);
}

//NO.2
function loadScript(url, callback){
    var xhr = new XMLHttpRequest();
    xhr.open("get", url, true);
    xhr.onreadystatechange = function(){
        if (xhr.readyState == 4){
            if (xhr.status >= 200 && xhr.status < 300 || xhr.status == 304){
                var script = document.createElement ("script");
                script.type = "text/javascript";
                script.text = xhr.responseText;
                document.body.appendChild(script);
                callback();
            }
        }
    };
    xhr.send(null);
}

dom加载完执行

function domLoad(fn){
    if(document.addEventListener){
        document.addEventListener("DOMContentLoaded", fn, false);
    }else{
        if(window.ActiveXObject){
            document.write("<script id='ie_onload' defer src='javascript:void(0)'><\/script>");
            document.getElementById("ie_onload").onreadystatechange = function(){
                if(this.readyState == "complete"){
                    this.onreadystatechange = null;
                    fn();
                }
            }
        }
        if(/WebKit/i.test(navigator.userAgent)){
            var _timer = setInterval(function(){
                if(/loaded|complete/.test(document.readyState)){
                    clearInterval(_timer);
                    fn();
                }
            }, 10);
        }
    }
}

加载样式表

function addSheet(url){
    var oLink = document.createElement('link'),oHead = document.getElementsByTagName('head')[0];
    oLink.rel = 'stylesheet';
    oLink.type = 'text/css';
    oLink.href = url;
    oHead.appendChild(oLink);
}

获取css

function getStyle(o, attr){
    if(o.currentStyle){
        return o.currentStyle[attr];
    }
    else{
        return getComputedStyle(o,false)[attr];
    }
}

获取服务器时间

function getNowDate(callback){
    var xhr = new XMLHttpRequest();
    xhr.open('get', 'null.txt', true); //null.txt不存在,我们不需要
    xhr.onreadystatechange = function(){
        if(xhr.readyState == 3){ //状态3响应
            callback(xhr.getResponseHeader('Date')); //返回时间,那么可以利用获得的时间做倒计时程序了。
        }
    };
    xhr.send(null);
}

cookie处理

function Cookie(name, value, options){
    if(typeof value != 'undefined'){
        options = options || {};
        if(value === null){
            options.expires = -1; //过期
        }
        var expires = '';
        //存在时间选项
        if(options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)){
            var date;
            if(typeof options.expires == 'number'){
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            }else{
                date = options.expires;
            }
            expires = '; expires='+date.toUTCString();
        }
        var path = options.path ? '; path='+options.path : '';
        var domain = options.domain ? '; domain='+options.domain : '';
        var secure = options.secure ? '; secure' : '';
        //写入cookie
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    }else{//读取cookie
        var cookValue = null;
        if(document.cookie && document.cookie != ''){
            var cookie = document.cookie.split(';');
            for(var i = 0, len = cookie.length; i < len; i++){
                var c = cookie[i].replace(/^\s+|\s+$/g, '');
                if(c.substring(0, name.length + 1) == (name + '=')){
                    cookValue = decodeURIComponent(c.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookValue;
    }
}
//设置
Cookie("user", "Jununx");
//获取
Cookie("user");
//删除
Cookie("user", null);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值