用 jQuery 封装 postMessage

发布于 2024-11-14 15:58:39 字数 1030 浏览 6 评论 0原文

我正在尝试学习 jQuery,我想制作一个简单的 postMessage 客户端。 可以用jquery封装吗?第一个警报有效,但第二个警报无效。

我正在尝试修改此代码:http://austinchau。 blogspot.com/2008/11/html5-cross-document-messaging.html

$(document).ready(function() {

   $('#submit_button').click(function() {
      var data = $('#message').val();
      window.postMessage('1st' + data);
      alert(data);
   });

    var onmessage = function(e) {
      var data = e.data;
      var origin = e.origin;
      document.getElementById('display').innerHTML = data;
      alert('2nd' + data);
    };

    if (typeof window.addEventListener != 'undefined') {
      window.addEventListener('message', onmessage, false);
    } else if (typeof window.attachEvent != 'undefined') {
      window.attachEvent('onmessage', onmessage);
    }
});

编辑:我知道有 许多插件可用于执行此操作,但我将其作为一个学习过程。

I'm trying to learn jQuery, I want to make a simple postMessage client.
Is it possible to wrap it in jquery? The first alert works, but the second does not.

I'm trying to modify this code: http://austinchau.blogspot.com/2008/11/html5-cross-document-messaging.html

$(document).ready(function() {

   $('#submit_button').click(function() {
      var data = $('#message').val();
      window.postMessage('1st' + data);
      alert(data);
   });

    var onmessage = function(e) {
      var data = e.data;
      var origin = e.origin;
      document.getElementById('display').innerHTML = data;
      alert('2nd' + data);
    };

    if (typeof window.addEventListener != 'undefined') {
      window.addEventListener('message', onmessage, false);
    } else if (typeof window.attachEvent != 'undefined') {
      window.attachEvent('onmessage', onmessage);
    }
});

Edit: I know there are many plugins available to do this, but I'm doing this as a learning process.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羁〃客ぐ 2024-11-21 15:58:39

postMessage 用于在不同页面或窗口(或 iframe)之间进行通信。您正在向同一个窗口发布消息。正如您所看到的,什么也没有发生。

如果您返回到原始博客文章,作者会创建并显示两个 iframe,然后在它们之间发布消息。

postMessage is used to communicate between different pages or windows (or iframes). You are posting a message to the same window. As you've seen, nothing happens.

If you go back to the original blog post, the author creates and displays two iframes and then posts messages between them.

在风中等你 2024-11-21 15:58:39

现在需要 postMessage 的第二个参数。您的代码可以更改为:

   window.postMessage('1st' + data, document.location);

其有效,因为您正在发布到自己的窗口。要发布到另一个窗口,请使用以下内容:

   popup = window.open('http://example.com/');
   popup.postMessage("Hi!", 'http://example.com/')

进一步阅读:

祝你好运!

The second argument to postMessage is now required. Your code could be changed to this:

   window.postMessage('1st' + data, document.location);

Which works because you are posting to your own window. To post to another window, use this:

   popup = window.open('http://example.com/');
   popup.postMessage("Hi!", 'http://example.com/')

Further Reading:

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文