JS获取跨域的cookie实例

如果说JS能实现跨域cookie,你可能觉得不太可能实现,不过事实上,这个还是可以搞定的,不过需要一定的条件才行的哦!具体方案如下:

一共需要3个文件,第一个文件为需要获取cookie的页面,在这个页面内嵌入存在网站B的获取cookie的代码,

第二个文件存在网站B,读取cookie,然后将自身URL修改为网站A域下,并将cookie拼接到URL中,

第三个文件在网站A,用于调用父页面处理得到的cookie.

代码如下:

文件一:假设为网站A下.1.html

<html>
<head>
</head>
<body>
<iframe src='http://B.com/2.html' width='100' height='100'>
</iframe>
<textarea id="sogou_cookie">
</textarea>
</body>
</html>

文件二:存在网站B下,名为2.html

<html>
<head>
</head>
<body>
<script>
window.location="http://A.com/3.html?"+document.cookie;
</script>
</body>
</html>

文件三:存在网站A下:

<html>
<head>
</head>
<body>
<script>
window.parent.document.getElementById("sogou_cookie").value=window.location.toString().substring(window.location.toString().indexOf("?"));
</script>
</body>
</html>

这样,当打开1.html时,文本框中内容就为B站的所有cookie了。

原文地址:https://www.cnblogs.com/BigIdiot/p/4434788.html