没事干的时候把凡客的首页扣下来研究了一下,在凡客的js代码里发现了一种跨域获取cookies的方法,基本原理是凡客的server端提供一个页面输出cookies字符串,client端采用jsonp的格式获取该cookies字符串。
你也可以在自己的机器上试下,不过你首先得在凡客上有帐号,这样才能看到效果。
下面一段是凡客中一个跨域获取cookies的函数:
01
|
function setWelcome() {
|
02
|
$.getScript("http://my.vancl.com/Usercenter/GetUserName.ashx", function() {
|
03
|
if (typeof (data) != undefined && typeof (data) != "undefined" && data != "") {
|
04
|
$("#login").html("<a class='top' href='https://login.vancl.com/login/UserLoginOut.aspx' target='_parent' >退出登录</a>");
|
05
|
$("#welcome").html("您好,<a class='top' href='http://my.vancl.com/' >" + data + "</a>。<a class='top' href='https://login.vancl.com/Login/UserLoginOut.aspx?'" + window.location.href +" target='_parent' >退出登录</a>");
|
06
|
}
|
07
|
else {
|
08
|
return;
|
09
|
}
|
10
|
}
|
11
|
);
|
本页的演示效果所用代码如下:
01
|
<div style="text-indent:2em;"><input type="button" value="运行" id="render" /></div>
|
02
|
<script type="text/javascript">
|
03
|
// <![CDATA[
|
04
|
document.getElementById('render').onclick=function(){
|
05
|
$.getScript("http://my.vancl.com/Usercenter/GetUserName.ashx", function()
|
06
|
{
|
07
|
if (typeof (data) != undefined && typeof (data) != "undefined" && data != "")
|
08
|
{
|
09
|
alert("你在凡客登录的用户名是:"+data);
|
10
|
}
|
11
|
else
|
12
|
{
|
13
|
alert("你还没有登录凡客!");
|
14
|
return;
|
15
|
}
|
16
|
});
|
17
|
}
|
18
|
// ]]>
|
19
|
</script>
|
在这里记录下~
热门源码