我有两种不同的网站siteA.com和siteB.com,当访问者浏览时需要共享一条信息siteA到siteB。我无法访问站点A的服务器端代码或导航链接,只能限制自定义设置和JavaScript。为了分享这些信息,我建立了一个完全由siteC.com控制的新页面,然后将此页面作为iframe添加到siteA和siteB。我正在使用postMessage
方法来从每个站点工作正常的iframe中获取和设置cookie,但实际上我最终得到了两个不同的cookie,每个站点A和站点B都有一个cookie,即使Cookie属于siteC,因为它是由iframe中的页面设置,通过F12调试器进行确认。我预计会有一个cookie,这两个网站可以通过iframe共享相同的cookie,我是否在这里丢失了某些东西,是否应该这样做,或者有另一种方法可以做到这一点?试图在使用常见iframe的域名之间共享cookie,但是却获得了两个cookie
这是我的网页在思泰科是被加载到iframe中
<!DOCTYPE html>
<html>
<head>
<title>iframe source</title>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
$(function() {
var ck = document.cookie;
var expDate = new Date();
expDate.setFullYear(expDate.getFullYear() + 20)
$("#auditlog").append("iframe loaded<br/>");
if (ck) {
$("#auditlog").append("cookie exists<br/>");
} else {
$("#auditlog").append("cookie not set<br/>");
}
// Assign handler to message event
if (window.addEventListener) {
window.addEventListener('message', messageHandler, false);
} else if (window.attachEvent) { // ie8
window.attachEvent('onmessage', messageHandler);
}
})
function messageHandler(e) {
var msg = {};
var response;
// Check origin
if (e.origin === 'http://siteA' || e.origin === 'http://siteB') {
// Retrieve data sent in postMessage
msg = JSON.parse(e.data);
if (msg.action == "getCookie") {
response = getCookie();
} else if (msg.action == "setCookie") {
setCookie(msg.payload);
response = "cookie set";
} else {
response = "action not supported";
}
// Send reply to source of message
e.source.postMessage(response, e.origin);
}
}
function setCookie(cookieVal) {
var expDate = new Date();
expDate.setFullYear(expDate.getFullYear() + 20)
document.cookie = cookieVal + "; expires=" + expDate.toUTCString();
}
function getCookie() {
return document.cookie;
}
</script>
</head>
<body>
<div id="auditlog"></div>
<div id="cookieinfo"></div>
</body>
</html>
的代码,这是我的站点A和站点B的网页代码,两者都使用相同的代码,这是一个示例我建立以测试set和get cookie的功能在iframe
<!DOCTYPE html>
<html>
<head>
<title>Main content page</title>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
$(function() {
// Assign handler to message event
if (window.addEventListener) {
window.addEventListener('message', messageHandler, false);
} else if (window.attachEvent) { // ie8
window.attachEvent('onmessage', messageHandler);
}
$("#btnGetIframeCookie").click(function() {
var postMsg = {
action:"getCookie"
}
// get reference to window inside the iframe
var wn = document.getElementById('cookieiframe').contentWindow;
// postMessage arguments: data to send, target origin
wn.postMessage(JSON.stringify(postMsg), 'http://siteC');
})
$("#btnSetIframeCookie").click(function() {
var cookieVal = $("#txtCookieValue").val();
var postMsg = {
action: "setCookie",
payload: cookieVal
}
var wn = document.getElementById('cookieiframe').contentWindow;
// postMessage arguments: data to send, target origin
wn.postMessage(JSON.stringify(postMsg), 'http://siteC');
})
})
function messageHandler(e) {
if (e.origin === 'http://siteC') {
$("#divMessages").append("response from iframe: <br/>" + e.data + "<br/>");
}
}
</script>
</head>
<body>
<div>
This is the iframe container
</div>
<div>
<input type="button" id="btnGetIframeCookie" value="Get iframe cookie" />
</div>
<div>
<input type="text" size="60" id="txtCookieValue" />
<input type="button" id="btnSetIframeCookie" value="Set iframe cookie" />
</div>
<iframe id="cookieiframe" src="http://siteC/iframe/index.html" style="width: 300px; height: 300px; border:1px solid black;"></iframe>
<div id="divMessages"></div>
</body>
</html>
使用这种设置,如果我通过与"keyabc=value123"
例如值的iframe,距离站点A一个cookie,然后我就可以读取相同cookie回来,但是当我去siteB有相同的pa ge在iframe那里,我没有cookie,直到我在那里设置一个,例如"keyabc=value456"
。现在,如果我查看我的实际cookie文件C:\Users\aakoehle\AppData\Local\Packages\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\AC\#!001\MicrosoftEdge\Cookies
,则会看到两个文件,一个包含我设置的每个值,并且都具有siteC的路径。我还为每个浏览器选项卡启动了F12工具,每个选项卡显示属于siteC的Cookie。
- 更新 -
随着我的代码贴在这里,现在我只看到了问题的cookie中的边缘浏览器的最新版本。按照预期,Chrome和IE在siteA和siteB之间共享一个cookie。
您是否尝试过其他方法,比如'localStorage'或'webSQL'或'sessionStorage'? – 11thdimension
我还没有尝试过这些选项,我对它们并不熟悉,但我会研究它,谢谢! – AK3800
我尝试了localStorage,但它的行为与我原来的cookie解决方案相同,我有两个值,每个页面托管iframe。 – AK3800