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了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM