如何将postMessage()与Google Chrome Frame ActiveX对象一起使用

时间:2012-04-30 23:22:38

标签: javascript jquery cross-domain google-chrome-frame

在使用Google Chrome Frame的activeX对象时,我遇到了使postMessage()工作的问题。这是我的情景:

我有一个父网页,由于我现在不会进入的原因,安装了Google Chrome浏览器内嵌框架。为了使用GCF打开 的外部页面,我需要通过在父页面中包含CFInstance.js文件来使用他们的activeX方法。我希望能够使用postMessage在父页面和子页面之间进行通信。

此处发布了一个示例:postMessage() with GCF

但是,我并没有完全遵循这个例子,因为它没有很好地解释如何使用ChromeFrame.postMessage()。 有没有人使用这种方法成功?

这是我的代码:

父页面:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title></title>
    <script type="text/javascript" src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'></script>
    <script type="text/javascript" src='https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js'></script>
    <script type="text/javascript" src='http://infodev-cms/EDWIntegrator/AdamS/CFInstance.js'></script>
    <script src='http://infodev-cms/EDWIntegrator/AdamS/jquery.json-2.3.min.js'></script>
    <script>
      $(document).ready(function() {
        var gcf = new CFInstance({
          src: "http://infodev-cms/EDWIntegrator/AdamS/gcf.html"
        });
        document.body.appendChild(gcf);   



          if (window.attachEvent) {
            window.attachEvent('onmessage', message);                        
          }                       


        function message(event) {
          var messageParams = $.parseJSON(event.data);
          alert(messageParams);
        }                   
      });
    </script>
    <style>
/*-------CSS Reset-------*/
     html, body, div, span, object, h1, h2, h3, h4, h5, h6, p, blockquote, pre,
     a, address, code, img, 
     small, strong, dl, dt, dd, ol, ul, li,
     fieldset, form, label {
         margin: 0;
         padding: 0;
         border: 0;
         outline: 0;
         font-size: 100%;
         vertical-align: baseline;
         background: transparent;
     }  
     html {
      height: 100%;
     }   
     body {
       height: 100%;
       overflow: hidden;
     }
     .chromeFrameDefaultStyle  {
       border: 0 none;
       width: 100%;
       height: 100%;
     }      
    </style>
  </head>
  <body>

  </body>
</html>

嵌套页面:

    <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
 <meta http-equiv="CACHE-CONTROL" content="NO-CACHE">
  <head>
    <title></title>
    <script src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'></script>
    <script src='https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js'></script>
    <script src='http://infodev-cms/EDWIntegrator/AdamS/json2.js'></script>
    <script src='http://infodev-cms/EDWIntegrator/AdamS/jquery.json-2.3.min.js'></script>
    <script type="text/javascript" src='http://infodev-cms/EDWIntegrator/AdamS/CFInstance.js'></script>    

    <script>
     (function() {
       $(document).ready(function() {
         var gcf = new CFInstance;

        $('#submit').on('click', function() {
          var data = {
           name: $('#name').val(),
           email: $('#email').val(),
           message: $('#message').val() 
          };

          window.parent.postMessage($.toJSON(data), '*');

        });

       });       
     })();

    </script>
    <style>
/*-------CSS Reset-------*/

body, div, h1, form, fieldset, input, textarea {
    margin: 0; padding: 0; border: 0; outline: none;
}

html {
    height: 100%;
}

body {
    background: #728eaa;
    background: -moz-linear-gradient(top, #25303C 0%, #728EAA 100%); /* firefox */  
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#25303C), color-stop(100%,#728EAA)); /* webkit */
    font-family: sans-serif; 
}

#contact {
    width: 430px; margin: 60px auto; padding: 60px 30px;
    background: #c9d0de; border: 1px solid #e1e1e1;
    -moz-box-shadow: 0px 0px 8px #444;
    -webkit-box-shadow: 0px 0px 8px #444;
    border-radius: 15px; -moz-border-radius: 15px; -webkit-border-radius: 15px;
}

h1 {
    font-size: 35px; color: #445668; text-transform: uppercase;
    text-align: center; margin: 0 0 35px 0; text-shadow: 0px 1px 0px #f2f2f2;
}

label {
    float: left; clear: left; margin: 11px 20px 0 0; width: 95px;
    text-align: right; font-size: 16px; color: #445668; 
    text-transform: uppercase; text-shadow: 0px 1px 0px #f2f2f2;
}

input {
    width: 260px; height: 35px; padding: 5px 20px 0px 20px; margin: 0 0 20px 0; 
    background: #5E768D;
    background: -moz-linear-gradient(top, #546A7F 0%, #5E768D 20%); /* firefox */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#546A7F), color-stop(20%,#5E768D)); /* webkit */
    border-radius: 5px; -moz-border-radius: 5px; -webkit-border-radius: 5px;
    -moz-box-shadow: 0px 1px 0px #f2f2f2;-webkit-box-shadow: 0px 1px 0px #f2f2f2;
    font-family: sans-serif; font-size: 16px; color: #f2f2f2; text-transform: uppercase; text-shadow: 0px -1px 0px #334f71; 
}
    input::-webkit-input-placeholder  {
        color: #a1b2c3; text-shadow: 0px -1px 0px #38506b;  
    }
    input:-moz-placeholder {
        color: #a1b2c3; text-shadow: 0px -1px 0px #38506b; 
    }

textarea {
    width: 260px; height: 170px; padding: 12px 20px 0px 20px; margin: 0 0 20px 0; 
    background: #5E768D;
    background: -moz-linear-gradient(top, #546A7F 0%, #5E768D 20%); /* firefox */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#546A7F), color-stop(20%,#5E768D)); /* webkit */
    border-radius: 5px; -moz-border-radius: 5px; -webkit-border-radius: 5px;
    -moz-box-shadow: 0px 1px 0px #f2f2f2;-webkit-box-shadow: 0px 1px 0px #f2f2f2;
    font-family: sans-serif; font-size: 16px; color: #f2f2f2; text-transform: uppercase; text-shadow: 0px -1px 0px #334f71; 
}
    textarea::-webkit-input-placeholder  {
        color: #a1b2c3; text-shadow: 0px -1px 0px #38506b;  
    }
    textarea:-moz-placeholder {
        color: #a1b2c3; text-shadow: 0px -1px 0px #38506b; 
    }

input:focus, textarea:focus {
    background: #728eaa;
    background: -moz-linear-gradient(top, #668099 0%, #728eaa 20%); /* firefox */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#668099), color-stop(20%,#728eaa)); /* webkit */
}

input[type=submit] {
    width: 185px; height: 52px; float: right; padding: 10px 15px; margin: 0 15px 0 0;
    -moz-box-shadow: 0px 0px 5px #999;-webkit-box-shadow: 0px 0px 5px #999;
    border: 1px solid #556f8c;
    background: -moz-linear-gradient(top, #718DA9 0%, #415D79 100%); /* firefox */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#718DA9), color-stop(100%,#415D79)); /* webkit */
    cursor: pointer;
}

    </style>
  </head>
 <body>

  <div id="contact">
    <h1>Send an email</h1>
    <form action="#" method="post">
        <fieldset>
            <label for="name">Name:</label>
            <input type="text" id="name" placeholder="Enter your full name" />

            <label for="email">Email:</label>
            <input type="email" id="email" placeholder="Enter your email address" />

            <label for="message">Message:</label>
            <textarea id="message" placeholder="What's on your mind?"></textarea>

            <input id="submit" type="submit" value="Send message" />

        </fieldset>
    </form>
  </div>

 </body>
</html>

非常感谢你的时间。另外,请原谅我在这篇文章中发现的任何明显的新手错误(代码相关或其他),这是我关于stackoverflow的第一个问题。

0 个答案:

没有答案