JavaScript操作cookie & Jquery跨域读取json数据
2010-08-25 22:49 ☆冷枫☆ 阅读(2817) 评论(0) 编辑 收藏 举报JavaScript读取cookie的操作以及Jquery跨域读取数据在平时的工作中用到的频率比较高,这里做下一个小的总结。
1.javascript操作cookie:
<script type="text/javascript">
<!--
var Cookie = {
// 设置Cookie值
setCookie: function(name, value, expires, path, domain){
document.cookie = name + "=" + escape( value ) +
( ( expires ) ? ";expires=" + expires.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" );
},
// 获取Cookie值
getCookie: function(name){
var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
if(arr != null) return unescape(arr[2]); return null;
},
// 删除Cookie值
delCookie: function(name){
var d = new Date();
d.setTime(d.getTime() - 3600 * 1000);
this.setCookie(name, "", d);
}
}
Cookie.setCookie("test",1); // 设置cookie为 name : test; value : 1
Cookie.delCookie("test") // 删除name为a的cookie
alert(Cookie.getCookie("test")); // 返回null 因为cookie已经删除
//-->
</script>
<!--
var Cookie = {
// 设置Cookie值
setCookie: function(name, value, expires, path, domain){
document.cookie = name + "=" + escape( value ) +
( ( expires ) ? ";expires=" + expires.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" );
},
// 获取Cookie值
getCookie: function(name){
var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
if(arr != null) return unescape(arr[2]); return null;
},
// 删除Cookie值
delCookie: function(name){
var d = new Date();
d.setTime(d.getTime() - 3600 * 1000);
this.setCookie(name, "", d);
}
}
Cookie.setCookie("test",1); // 设置cookie为 name : test; value : 1
Cookie.delCookie("test") // 删除name为a的cookie
alert(Cookie.getCookie("test")); // 返回null 因为cookie已经删除
//-->
</script>
2.Jquery跨域读取数据json数据:
<html>
<head>
<title>JQueryѧϰ</title>
<script src="jquery-1.4.2.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
var oBtnTest = $("#btnTest");
oBtnTest.click(function(){
oBtnTest.disabled = true;
var oResult = $("#result");
oResult.html("loading").css("color","red");
jQuery.getScript("http://app.cntvs.com/test/js.txt",
function(){
oResult.html("name:" + jimmy.name + "<br/>email:" + jimmy.email).css("color","black");
oBtnTest.disabled = false;
});
});
});
</script>
</head>
<body>
<button id="btnTest">BtnTest</button>
<div id="result"></div>
</body>
</html>
<head>
<title>JQueryѧϰ</title>
<script src="jquery-1.4.2.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
var oBtnTest = $("#btnTest");
oBtnTest.click(function(){
oBtnTest.disabled = true;
var oResult = $("#result");
oResult.html("loading").css("color","red");
jQuery.getScript("http://app.cntvs.com/test/js.txt",
function(){
oResult.html("name:" + jimmy.name + "<br/>email:" + jimmy.email).css("color","black");
oBtnTest.disabled = false;
});
});
});
</script>
</head>
<body>
<button id="btnTest">BtnTest</button>
<div id="result"></div>
</body>
</html>
返回数据:
name:jimmy.yang
email:jimmy.yang@163.com
email:jimmy.yang@163.com
原始json数据:
var jimmy = {name:"jimmy.yang",email:jimmy.yang@163.com}
励志博客园--优秀的阅读、励志、交流学习平台。您的网上心灵家园!