简体   繁体   English

从window.onmessage上的消息获取数据

[英]Get data from message on window.onmessage

I have html frame which i need to pass data on message code it is already passed my all data in text box but as one block when i want to get each text box value sperately 我有html框架,我需要在消息代码上传递数据,它已经传递了文本框中的所有数据,但是当我想分别获取每个文本框值时作为一个块

I have make an array and pushed the data into that array but my problem that array is repeated for example if i have four text boxes then the result will look like this one while i need only the last array 我已经制作了一个数组并将数据推送到该数组中,但是我的问题是重复数组,例如,如果我有四个文本框,则结果将看起来像这样,而我只需要最后一个数组

 (1)["eeeeee"]
(2) ["eeeeee", "rrrrrrrrrr"]
 (3) ["eeeeee", "rrrrrrrrrr", "tttttttt"]
(4) ["eeeeee", "rrrrrrrrrr", "tttttttt", "yyyyyy"]



   <script type="text/javascript">

      // when a message is received from the page code

     // when a message is received from the page code
      var arr = [];
      window.onmessage = (event) => {
          // debugger;
           if (event.data) {
          document.getElementById("theLabel").innerHTML = event.data;
          var receivedData = event.data;

            arr.push(receivedData);
        }
            console.log(arr);
      };

 // send message to the page code

      function button_click() {

          window.parent.postMessage(document.getElementById("theMessage").value,"*");
window.parent.postMessage(document.getElementById("theMessage1").value, "*");
window.parent.postMessage(document.getElementById("theMessage2").value, "*");
window.parent.postMessage(document.getElementById("theMessage3").value, "*");

      }

    </script>

I expect to get only that result (4) ["eeeeee", "rrrrrrrrrr", "tttttttt", "yyyyyy"] 我希望只得到该结果(4)[“ eeeeee”,“ rrrrrrrrrr”,“ tttttttt”,“ yyyyyy”]

If I understand you well you want do this to send the data. 如果我对您的了解很好,则希望发送数据。

function button_click() {
    window.parent.postMessage(document.getElementById("theMessage").value,"*");
    window.parent.postMessage(document.getElementById("theMessage1").value, "*");
    window.parent.postMessage(document.getElementById("theMessage2").value, "*");
    window.parent.postMessage(document.getElementById("theMessage3").value, "*");
}

And you try to read the send data with this 然后您尝试以此读取发送数据

var arr = [];
window.onmessage = (event) => {
    if (event.data) {
        document.getElementById("theLabel").innerHTML = event.data;
        var receivedData = event.data;
        arr.push(receivedData);
    }
    console.log(arr);
};

And you wonder why you see this in the console? 您想知道为什么在控制台中看到了吗?

(1)["eeeeee"]
(2) ["eeeeee", "rrrrrrrrrr"]
(3) ["eeeeee", "rrrrrrrrrr", "tttttttt"]
(4) ["eeeeee", "rrrrrrrrrr", "tttttttt", "yyyyyy"]

Well it is because you write the complete array to the console each time you receive a message. 那是因为每次收到消息时,您都会将完整的数组写入控制台。 What you could do is wait till you have 4 messages and than write it to the console like this. 您可以做的就是等到收到4条消息,然后再像这样将其写入控制台。

var arr = [];
window.onmessage = (event) => {
    if (event.data) {
        document.getElementById("theLabel").innerHTML = event.data;
        var receivedData = event.data;
        arr.push(receivedData);
    }
    if(arr.length === 4) {
        console.log(arr);
    }
};

Or what is probably better to just send all the data together in one message for example as an object. 或者最好将所有数据一起发送到一条消息中,例如作为一个对象。

function button_click() {
    var messages = {
        theMessage: document.getElementById("theMessage").value,
        theMessage2: document.getElementById("theMessage").value,
        theMessage3: document.getElementById("theMessage").value,
        theMessage4: document.getElementById("theMessage").value
    }
    window.parent.postMessage(messages,"*");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM