Sending Data To Another Domain Using PostMessage

In this post we will see how to send data to another domain from our application.

In this post we will see how to send data to another domain from our application. You can simply call it cross domain data sharing. In few cases we may need to send some data which is needed for our other domain or application to work as expected. I had some cases in my programming life. I have done this requirement with the help of the postMessage method. The window.postMessage method helps us to enables cross-origin communication. I hope you will like this.

Background

I was forced to maintain some cookies in my second application every time. I wanted to set that the same from my first application as well. So I implemented this with the help of window.postMessage. For the demo purpose, here I am creating two applications.

  • CrossDomainDataSharing
  • Receiver

Using the code

First, we will concentrate on the CrossDomainDataSharing application in which we are sending our data toReceiver application.

To start with you need to add jQuery reference to your page. And to make this application trendy, I have added Smart Alert to our application, you can see the needed references and styles below.

  1. <script src="Scripts/jquery-2.2.0.min.js"></script>  
  2. <script src="js/alert.min.js"></script>  
  3. <link href="css/alert.min.css" rel="stylesheet" />  
  4. <link href="themes/dark/theme.css" rel="stylesheet" />  
Next thing is “Calling Window On Load” function.
  1. window.onload = function () {  
  2.    document.getElementById('btnSend').addEventListener('click', sendData);  
  3. };  
Here we are just adding a click event to the button with id btnSend. Before going to add the event, please make sure that you have created a button.
  1. <button id="btnSend">Send Feedback</button>  
The next thing is to create an iframe and load our second application link to that by giving src value.
  1. <iframe id="ifrLoad" src="http://localhost:55592/Receiver/Cross-Domain-Data-Sharing-Receiver.html" style="display: none;">  
  2.    <p>Oops!. Your browser does not support iframes.</p>  
  3. </iframe>  
Have you noticed that we have called a function called sendData? Let us see what that function does.
  1. function sendData(e) {  
  2.     try {  
  3.         $.alert.open('prompt''Please send your feedback!.', function (button, value) {  
  4.             if (button == 'ok') {  
  5.                 e.preventDefault();;  
  6.                 var myIfr = window.frames['ifrLoad'].contentWindow;  
  7.                 myIfr.postMessage(value, 'http://localhost:55592/Receiver/Cross-Domain-Data-Sharing-Receiver.html')  
  8.                 $.alert.open('Thank you so much for your feedback.' || 'No value has been entered');  
  9.             }  
  10.         });  
  11.     }   
  12.     catch (e) {  
  13.         console.log('Error: ' + e.message);  
  14.     }  
  15. }  
We are creating a smart alert first if the user says ‘Ok,’ we will just pass the given feedback to our second application with the help of the postMessage method. The implementation is pretty simple, isn’t it?

You can style the button you have created as follows.
  1. <style>  
  2.     #btnSend {  
  3.         margin-left11px;  
  4.         bordersolid 1px #000;  
  5.         padding9px 22px 7px;  
  6.         min-width32px;  
  7.         font-weightbold;  
  8.         line-height13px;  
  9.         color#fff;  
  10.         text-shadow0 1px 1px rgba(0000.5);  
  11.         background-color#232323;  
  12.         background-image: linear-gradient(#2b2b2b 1%#1d1d1d 99%);  
  13.         background-image: -webkit-linear-gradient(#2b2b2b 1%#1d1d1d 99%);  
  14.         background-image: -moz-linear-gradient(#2b2b2b 1%#1d1d1d 99%);  
  15.         background-image: -o-linear-gradient(#2b2b2b 1%#1d1d1d 99%);  
  16.         background-image: -ms-linear-gradient(#2b2b2b 1%#1d1d1d 99%);  
  17.         -pie-background: linear-gradient(#2b2b2b 1%#1d1d1d 99%);  
  18.         border-radius: 3px;  
  19.         -webkit-border-radius: 3px;  
  20.         -moz-border-radius: 3px;  
  21.         -ms-border-radius: 3px;  
  22.         box-shadow: 0 1px rgba(2330.5), 0 1px rgba(2552552550.07inset0 0 01px rgba(2552552550.06inset;  
  23.         -webkit-box-shadow: 0 1px rgba(2330.5), 0 1px rgba(2552552550.07inset0 0 0 1px rgba(2552552550.06inset;  
  24.         -moz-box-shadow: 0 1px rgba(2330.5), 0 1px rgba(2552552550.07inset0 00 1px rgba(2552552550.06inset;  
  25.         -ms-box-shadow: 0 1px rgba(2330.5), 0 1px rgba(2552552550.07inset0 00 1px rgba(2552552550.06inset;  
  26.         -o-box-shadow: 0 1px rgba(2330.5), 0 1px rgba(2552552550.07inset0 0 01px rgba(2552552550.06inset;  
  27.     }  
  28.   
  29.     #btnSend:hover {  
  30.         background-color#404040;  
  31.         background-image: linear-gradient(#515151 1%, #2e2e2e);  
  32.         background-image: -webkit-linear-gradient(#515151 1%, #2e2e2e);  
  33.         background-image: -moz-linear-gradient(#515151 1%, #2e2e2e);  
  34.         background-image: -o-linear-gradient(#515151 1%, #2e2e2e);  
  35.         background-image: -ms-linear-gradient(#515151 1%, #2e2e2e);  
  36.         -pie-background: linear-gradient(#515151 1%, #2e2e2e);  
  37.         box-shadow: 0 1px rgba(2330.5), 0 1px rgba(2552552550.07inset0 0 01px rgba(2552552550.06inset;  
  38.         -webkit-box-shadow: 0 1px rgba(2330.5), 0 1px rgba(2552552550.07inset0 0 0 1px rgba(2552552550.06inset;  
  39.         -moz-box-shadow: 0 1px rgba(2330.5), 0 1px rgba(2552552550.07inset0 00 1px rgba(2552552550.06inset;  
  40.         -ms-box-shadow: 0 1px rgba(2330.5), 0 1px rgba(2552552550.07inset0 00 1px rgba(2552552550.06inset;  
  41.         -o-box-shadow: 0 1px rgba(2330.5), 0 1px rgba(2552552550.07inset0 0 01px rgba(2552552550.06inset;  
  42.     }  
  43. </style>  
Now that is all for the sending part, please see the complete code for our first application here.
  1. <!DOCTYPE html>  
  2. <html>  
  3.   
  4.     <head>  
  5.         <title>Cross Domain Data Sharing Demo - Sibeesh Passion</title>  
  6.         <script src="Scripts/jquery-2.2.0.min.js"></script>  
  7.         <script src="js/alert.min.js"></script>  
  8.         <link href="css/alert.min.css" rel="stylesheet" />  
  9.         <link href="themes/dark/theme.css" rel="stylesheet" />  
  10.         <style>  
  11.         #btnSend {  
  12.             margin-left11px;  
  13.             bordersolid 1px #000;  
  14.             padding9px 22px 7px;  
  15.             min-width32px;  
  16.             font-weightbold;  
  17.             line-height13px;  
  18.             color#fff;  
  19.             text-shadow0 1px 1px rgba(0000.5);  
  20.             background-color#232323;  
  21.             background-image: linear-gradient(#2b2b2b 1%#1d1d1d 99%);  
  22.             background-image: -webkit-linear-gradient(#2b2b2b 1%#1d1d1d 99%);  
  23.             background-image: -moz-linear-gradient(#2b2b2b 1%#1d1d1d 99%);  
  24.             background-image: -o-linear-gradient(#2b2b2b 1%#1d1d1d 99%);  
  25.             background-image: -ms-linear-gradient(#2b2b2b 1%#1d1d1d 99%);  
  26.             -pie-background: linear-gradient(#2b2b2b 1%#1d1d1d 99%);  
  27.             border-radius: 3px;  
  28.             -webkit-border-radius: 3px;  
  29.             -moz-border-radius: 3px;  
  30.             -ms-border-radius: 3px;  
  31.             box-shadow: 0 1px rgba(2330.5), 0 1px rgba(2552552550.07inset0 0 0 1px rgba(2552552550.06inset;  
  32.             -webkit-box-shadow: 0 1px rgba(2330.5), 0 1px rgba(2552552550.07inset0 0 0 1px rgba(2552552550.06inset;  
  33.             -moz-box-shadow: 0 1px rgba(2330.5), 0 1px rgba(2552552550.07inset0 0 0 1px rgba(2552552550.06inset;  
  34.             -ms-box-shadow: 0 1px rgba(2330.5), 0 1px rgba(2552552550.07inset0 0 0 1px rgba(2552552550.06inset;  
  35.             -o-box-shadow: 0 1px rgba(2330.5), 0 1px rgba(2552552550.07inset0 0 0 1px rgba(2552552550.06inset;  
  36.         }  
  37.           
  38.         #btnSend:hover {  
  39.             background-color#404040;  
  40.             background-image: linear-gradient(#515151 1%, #2e2e2e);  
  41.             background-image: -webkit-linear-gradient(#515151 1%, #2e2e2e);  
  42.             background-image: -moz-linear-gradient(#515151 1%, #2e2e2e);  
  43.             background-image: -o-linear-gradient(#515151 1%, #2e2e2e);  
  44.             background-image: -ms-linear-gradient(#515151 1%, #2e2e2e);  
  45.             -pie-background: linear-gradient(#515151 1%, #2e2e2e);  
  46.             box-shadow: 0 1px rgba(2330.5), 0 1px rgba(2552552550.07inset0 0 0 1px rgba(2552552550.06inset;  
  47.             -webkit-box-shadow: 0 1px rgba(2330.5), 0 1px rgba(2552552550.07inset0 0 0 1px rgba(2552552550.06inset;  
  48.             -moz-box-shadow: 0 1px rgba(2330.5), 0 1px rgba(2552552550.07inset0 0 0 1px rgba(2552552550.06inset;  
  49.             -ms-box-shadow: 0 1px rgba(2330.5), 0 1px rgba(2552552550.07inset0 0 0 1px rgba(2552552550.06inset;  
  50.             -o-box-shadow: 0 1px rgba(2330.5), 0 1px rgba(2552552550.07inset0 0 0 1px rgba(2552552550.06inset;  
  51.         }  
  52.         </style>  
  53.         <script>  
  54.         window.onload = function () {  
  55.             document.getElementById('btnSend').addEventListener('click', sendData);  
  56.         };  
  57.   
  58.         function sendData(e) {  
  59.             try {  
  60.                 $.alert.open('prompt''Please send your feedback!.', function (button, value) {  
  61.                     if (button == 'ok') {  
  62.                         e.preventDefault();;  
  63.                         var myIfr = window.frames['ifrLoad'].contentWindow;  
  64.                         myIfr.postMessage(value, 'http://localhost:55592/Receiver/Cross-Domain-Data-Sharing-Receiver.html')  
  65.                         $.alert.open('Thank you so much for your feedback.' || 'No value has been entered');  
  66.                     }  
  67.                 });  
  68.             } catch (e) {  
  69.                 console.log('Error: ' + e.message);  
  70.             }  
  71.         }  
  72.         </script>  
  73.     </head>  
  74.   
  75.     <body> <button id="btnSend">Send Feedback</button> <iframe id="ifrLoad" src="http://localhost:55592/Receiver/Cross-Domain-Data-Sharing-Receiver.html" style="display: none;">  
  76. <p>Oops!. Your browser does not support iframes.</p>  
  77. </iframe> </body>  
  78.   
  79. </html>  
Now we will concentrate on our second application 'Receiver.'

We will just add a window load function with event listener which accepts the data from our first application. Please see the following code for further clarification.
  1. $(window).load(function () {  
  2.     window.addEventListener('message', addData);  
  3.   
  4.     function addData(e) {  
  5.         document.cookie = "myData=" + e.data;  
  6.     };  
  7. });  
Here myData is the cookie name.

You can see the complete code for our second application here.
  1. <!DOCTYPE html>  
  2. <html>  
  3.   
  4.     <head>  
  5.         <title>Cross Domain Data Sharing Receiver - Sibeesh Passion</title>  
  6.         <script src="Scripts/jquery-2.2.0.min.js"></script>  
  7.         <script>  
  8.         $(window).load(function () {  
  9.             window.addEventListener('message', addData);  
  10.   
  11.             function addData(e) {  
  12.                 document.cookie = "myData=" + e.data;  
  13.             };  
  14.         });  
  15.         </script>  
  16.     </head>  
  17.   
  18.     <body> </body>  
  19.   
  20. </html>  
Now it is time to see our output.

Output

While sending the data to our second application.

Prompt

Message

While receiving the data in our second application.

Localhost

That is all. We did it. Have a happy coding.

Conclusion

Did I miss anything that you may think is needed? Have you ever wanted to do this requirement? Did you find this post useful? I hope you liked this article. Please share with me your valuable suggestions and feedback.

Your turn. What do you thin?

A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.

Please see this article in my blog here.

Download Source Code