Window A is any website. The user runs a bookmarklet in this window which downloads a script from my domain. Window B is on my website and has the same domain as the script. What is the best way to send a message from window A to window B when the user runs the bookmarklet?
I was able to get this working by using window.postMessage
to communate to a hidden iframe on my domain which used the localstorage api to forward the message to window B. However, I want to know if a simpler method exists.
Method That Worked: Window A -> postMessage -> Hidden iframe on my domain in window A -> storage apis -> Window B
Is there a way to go directly from window A to window B without a hidden iframe or server side logic?
If there is no connection between windows, which means domain A is not an iframe of domain B and vice versa (and you do not have control over both), than you have to use server side logic. The hidden iframe you used is one way, the other would be following:
You can call the site B periodically or intentionally using script in Window A as:
var cookieScriptUrl = 'http://www.siteB.com/set_cookie.php?';
function sendData( data ) {
var image = new Image();
image.src = cookieScriptUrl + data;
};
sendData('likes=3');
You can send a lot of data, keeping in mind the max is around 2Mb.
Further on, in your PHP script set the cookie(s), than you can read them in Window B.