当前位置:编程学习 > JS >>

js 如何通过js脚本动态加载js文件及读写cookie

在前端开发中,我们不免用到读写cookie,动态引入js脚本等相关操作,怎么实现呢?请参阅下面的js函数:

[javascript] var JsHelper={ 
    CreateJS: function(url) {  
        var h = document.getElementsByTagName("head")[0]; 
        var s = document.createElement("SCRIPT"); 
        s.charset = "UTF-8"; 
        s.src = url; 
        h.appendChild(s); 
    }, 
        SetCookieByDays: function(name, value, Days, domainValue) { 
            var cookieValue = name + "=" + escape(value); 
            if (domainValue != null)cookieValue += "; 
        domain=" + domainValue; 
            if (Days != null && Days != 0) { 
            var exp = new Date(); 
            exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);  
            cookieValue += ";expires=" + exp.toGMTString(); 
        } 
            document.cookie = cookieValue; 
        }, 
       GetCookie: function(name) { 
        var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)")); 
        if (arr != null) return unescape(arr[2]);  
        return null; 
    }, 
        QueryString: function(item) { 
        var sValue = location.search.match(new RegExp("[\?\&]" + item + "=([^\&]*)(\&?)", "i")); 
        return sValue ? sValue[1] : sValue 
    } 
}; 

 摘自 心灵净土

补充:web前端 , JavaScript ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,