JS獲取跨域的cookie實例

若是說JS能實現跨域cookie,你可能以爲不太可能實現,不過事實上,這個仍是能夠搞定的,不過須要必定的條件才行的哦!具體方案以下:html

一共須要3個文件,第一個文件爲須要獲取cookie的頁面,在這個頁面內嵌入存在網站B的獲取cookie的代碼,跨域

第二個文件存在網站B,讀取cookie,而後將自身URL修改成網站A域下,並將cookie拼接到URL中,cookie

第三個文件在網站A,用於調用父頁面處理獲得的cookie.網站

代碼以下:htm

文件一:假設爲網站A下.1.htmlip

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

 

文件二:存在網站B下,名爲2.htmliframe

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

 

文件三:存在網站A下:io

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

相關文章
相關標籤/搜索