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
}
};
摘自 心灵净土
相关新闻>>
- 发表评论
-
- 最新评论 更多>>