-2

Basically, I want to do something like this:

var a = 3;
var b = 4;
postMessage(a, '*');
postMessage(b, '*');

Then have a listener do something like this:

addEventListener('message', event => {
sessionStorage.setItem("variable a",event.data=a);
sessionStorage.setItem("variable b",event.data=b);

});

Evidently "event.data=a" and "event.data=b" are not real code, but I hope the meaning gets across...

Thanks!

Pat D.
  • 1
  • Pass the name of the variable explicitly, if you care about it. – Bergi Nov 13 '21 at 04:30
  • @Bergi this is EXACTLY what I really want to do... so how do I pass the name of the variable through the postMessage explicitly and capture it in the addEventListener? Programming simpleton over here... sorry... – Pat D. Nov 14 '21 at 05:13
  • `postMessage({name: "a", value: a})`. Then you can ``console.log(`variable ${event.data.name} = ${event.data.value}`)`` – Bergi Nov 14 '21 at 05:16
  • Cool... I understand how this works now. I had indeed seen something like this before, but failed to capture its significance. I appreciate the clarification. – Pat D. Nov 14 '21 at 05:26

1 Answers1

0

If the types are the same, not really, with two addEventListeners - instead, consider posting an object composed of both a and b, and storing the stringified object in storage instead. This not only solves your problem, but it's also a much more manageable and elegant approach.

postMessage({ a, b }, '*');

and

addEventListener('message', event => {
  sessionStorage.setItem('datas', JSON.stringify(event.data));
});
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • This is wicked awesome... thanks! Your simple solution also made me realize that I could have just postMessaged an array which would have been a way of accomplishing what I was trying to do too I suppose. Wasn't obvious to me at first... – Pat D. Nov 14 '21 at 05:12
  • When an answer solves your problem, you may consider upvoting and/or marking it as Accepted (check the checkbox on the left) to indicate that the issue is resolved :) – CertainPerformance Nov 14 '21 at 05:31
  • I did upvote, but don't have enough reputation points for it to show (sorry). I didn't "accept this answer" yet, because it doesn't answer my question explicitly, even if it was extremely useful. I think @Bergi in the comments above more closely captured the essence of my question. Ultimately though, I would like to define var a = 3; in an iframe and pass ALL that information to the parent window (i.e. that "a = 3"), preferably without needing to set a new variable in the parent window. Not sure if that is possible or not. I suppose that is not what I am saying with the sessionStorage bit.... – Pat D. Nov 14 '21 at 05:39
  • I also want to transfer "a = 3" across web pages within a browser tab. e.g. set var a = 3; in an iframe, send that info to its parent window, then retain that info when I click to another page within the same browser tab so that I can then use "a = 3" within that new page. It likely makes more sense in my head then on paper... sorry – Pat D. Nov 14 '21 at 05:44
  • I should also add that I need to do this for multiple variables in the same iframe and update the values of those variables in the parent window as they change in the iframe before going to the new page. – Pat D. Nov 14 '21 at 05:53
  • Maybe your answer did answer my question explicitly as it was posed and my question didn't mean what I thought it did... if that is the case, then I do owe you an "accept answer"... let me know... and I will repost a better question on another thread (says the crazy person who is having a conversation alone on the internet). – Pat D. Nov 14 '21 at 05:57