Windows Store App和iFrame cookie [英] Windows Store App and iFrame cookie

查看:88
本文介绍了Windows Store App和iFrame cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个托管iframe的Javascript Windows Store应用程序。 iframe内的网页无法识别我的应用程序,因此该应用程序没有任何 PostMessage 界面。

I have a Javascript Windows Store Application that hosts an iframe. The webpage inside of my iframe is not aware of my app, so it doesn't have any PostMessage interface to my app.

当我在iframe中登录时,iframe将设置一个cookie。但是在我的应用中,我不相信有一种方法可以获取iframe cookie。

The iframe will set a cookie when I 'log in' in the iframe. But in my app, I don't believe that there is a way that I can get the iframe cookie. Is this correct?

如果网页知道我的应用程序怎么办? (即,我可以在iframe中更改网页)。

What if the web page knows about my application? (i.e. I can change the webpage in the iframe) Do I use PostMessage?

推荐答案

您可以使用 PostMessage ,您的主页将收到该消息。

You can use PostMessage that your main page will receive the message.

这是Win8 Developer Preview中的有效示例:

Here is working example in Win8 Developer Preview:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=1024, height=768" />
<title>WinWebApp1</title>
<!-- WinJS references -->
<link rel="stylesheet" href="/winjs/css/ui-dark.css" />
<script src="/winjs/js/base.js"></script>
<script src="/winjs/js/wwaapp.js"></script>
<script src="/winjs/js/ui.js"></script>
<!-- WinWebApp3 references -->
<link rel="stylesheet" href="/css/default.css" />
<script src="/js/default.js"></script>
        <script type="text/javascript">

            window.attachEvent("onmessage", receiveMessage);

            function receiveMessage(e) {
                if (e.origin == "http://www.scrumpt.com")
                    document.getElementById("target-element-id").innerHTML = e.data;

            }
</script>
</head>
<body>
    <iframe src="http://www.scrumpt.com/frametest2.html" style="display: block; width: 699px; height: 296.95px; left: -499px; top: 0px;"></iframe>
<div data-win-control="WinJS.UI.ViewBox" style="display: block; top: 50%;">
        <div class="fixed-layout">
        <div id="target-element-id">Click on Send Message above</div>
    </div>
    </div>
</body>
</html>

在服务器上(此主机现在在 http://www.scrumpt.com/frametest2.html )您拥有:

On the server (it is live at this moment on http://www.scrumpt.com/frametest2.html) you have:

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function send() {
      parent.postMessage('hello world', '*');
}
</script>
</head>
<body>
<a href="javascript:send()">Send message</a>
</body>
</html>

请确保您的div( target-element-id)具有正确的ID,如果您复制粘贴上面的代码。粘贴时,VS可能会将id更改为 Div1。

make sure that your div ("target-element-id") has the correct id when if you copy paste the code above. VS might change the id to "Div1" when pasting.

这篇关于Windows Store App和iFrame cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆