无法使iframe.contentwindow / postmessage生效,因为同一域

时间:2015-09-27 00:01:14

标签: javascript visual-studio iframe msdn postmessage

我无法获得最简单的iframe contentwindow代码,因为始终拒绝对contentwindow的权限。在使用我自己的代码到达WTF阶段后,我将我的尝试分开,在下面的示例中,我仅使用Microsoft自己的MSDN帮助iframe contentwindow文档中的示例代码。 MSDN的代码有同样的问题! (实际上它有两个问题,但我修改了使用=“iframe”=“IFRAME”,因为“iframe”!=“IFRAME”。)

我正在使用Javascript-Windows 8-Windows模板在Visual Studio Community 2015中进行开发。

在调试中,我看到每当我使用iframeid.contentwindow构造时,我都会被拒绝,如本帖末尾的屏幕截图所示。

这是我的代码:

1)default.html :(直接从文档中粘贴到VS中)

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>App3</title>

    <!-- WinJS references -->
    <link href="//Microsoft.WinJS.2.0/css/ui-dark.css" rel="stylesheet"                         />
    <script src="//Microsoft.WinJS.2.0/js/base.js"></script>
    <script src="//Microsoft.WinJS.2.0/js/ui.js"></script>

     <!-- App3 references --        >
     <link href="/css/default.css" rel="stylesheet" />

     <script type="text/javascript">
          function fnNavigate() {
                for (i = 0; i < document.all.length; i++) {
          if (document.all(i).tagName == "IFRAME") {
            document.all(i).contentWindow.location = "http://www.msn.com";
          }
        }
      }
     </script>
     <script src="/js/default.js"></script>
    </head>
    <body>
     <p> start
    <button onclick="fnNavigate();">Navigate Frames</button>
    <p>
    <iframe src="http://www.microsoft.com" style="width:100%;height:150px;">                         </iframe>
    <p />
    <iframe src="http://www.microsoft.com" style="width:100%;height:150px;"></iframe>
     <p />
     <iframe src="http://www.microsoft.com" style="width:100%;height:150px;"></iframe>
     <p /> end 
</body>
</html>

和2)default.js - 与VS模板开箱即用生成的内容相同

    (function () {
        "use strict";

        var app = WinJS.Application;
        var activation = Windows.ApplicationModel.Activation;

        app.onactivated = function (args) {
            if (args.detail.kind === activation.ActivationKind.launch) {
                if (args.detail.previousExecutionState !==         activation.ApplicationExecutionState.terminated) {
            // TODO: This application has been newly launched. Initialize
            // your application here.
        } else {
            // TODO: This application was suspended and then terminated.
            // To create a smooth user experience, restore application state         here so that it looks like the app never stopped running.
        }
        args.setPromise(WinJS.UI.processAll());
    }
};

        app.oncheckpoint = function (args) {
            // TODO: This application is about to be suspended. Save any state
            // that needs to persist across suspensions here. You might use the
            // WinJS.Application.sessionState object, which is automatically
            // saved and restored across suspension. If you need to complete an
            // asynchronous operation before your application is suspended, call
            // args.setPromise().
        };

        app.start();
    })();

尽管发现了几个看似相似的帖子,但我发现没有任何帮助。很多帖子讨论了AJAX,跨域起源,CORS,对iFrames的浏览器支持...... - 我没有那么复杂。我现在只在VS调试器中运行它,并打算将该代码用作1层本地应用程序或部署用于本地IIS / Chrome使用。我有一种感觉,要么它永远不可能访问iframe的内容窗口,要么我错过了一些简单的东西。我怀疑是后者。如果您知道答案,我将非常感谢您的帮助。 ;&GT) 谢谢

Screen shot of Error:

0 个答案:

没有答案