最近在发现使用Taobao的时候的一个小细节,于是便萌发起了写这篇文章。
当我们在 www.taobao.com 中进行登录之后,然后直接切换到 www.tmall.com 域名下,发现www.tmall.com首页的最顶部马上显示成了”您好, andyfaces“,于是便对此处的实现机制进行分析。 首先,用户名应该是存储在cookie中的,于是在taobao.com的域名中用 firefox看到用户名确实是存储在 cookie, 而tmall.com中没有存储该cookie: 可以确定的是对于cookie来说肯定是不允许垮域访问的。无论是通过JS还是Server端程序来说都是如此,那么tmall.com是如何访问到taobao.com下的cookie的呢? 于是打开 tmall.com,然后使用firebug来进行调试,发现了一条这样的请求语句, 其页面的JS代码为:- <script>
- KISSY.getScript("http://www.taobao.com/go/app/tmall/login-api.php?"+Math.random())
- </script>
- getScript: function(url, success, charset) {
- var isCSS = RE_CSS.test(url),
- node = doc.createElement(isCSS ? 'link' : 'script'),
- config = success, error, timeout, timer;
- node.src = url;
- node.async = true;
- scriptOnload(node, function() {
- if (timer) {
- timer.cancel();
- timer = undef;
- }
- S.isFunction(success) && success.call(node);
- // remove script
- if (head && node.parentNode) {
- head.removeChild(node);
- }
- });
- head.insertBefore(node, head.firstChild);
- }
- $.getScript('http://www.taobao.com/go/app/tmall/login-api.php?0.6783450077710154', function(){
- console.log("the taobao.com cookie object:" + userCookie + " username:" + userCookie._nk_);
- });
http://www.iteye.com/topic/1000776