<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>postMessage &#8211; Sibeesh Passion</title>
	<atom:link href="https://mail.sibeeshpassion.com/tag/postmessage/feed/" rel="self" type="application/rss+xml" />
	<link>https://mail.sibeeshpassion.com</link>
	<description>My passion towards life</description>
	<lastBuildDate>Sat, 14 Jul 2018 05:59:14 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>/wp-content/uploads/2017/04/Sibeesh_Passion_Logo_Small.png</url>
	<title>postMessage &#8211; Sibeesh Passion</title>
	<link>https://mail.sibeeshpassion.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Sending data to another domain usign postMessage</title>
		<link>https://mail.sibeeshpassion.com/sending-data-to-another-domain-usign-postmessage/</link>
					<comments>https://mail.sibeeshpassion.com/sending-data-to-another-domain-usign-postmessage/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Tue, 02 Feb 2016 00:00:05 +0000</pubDate>
				<category><![CDATA[How to]]></category>
		<category><![CDATA[cross domain data sharing]]></category>
		<category><![CDATA[cross-origin communication]]></category>
		<category><![CDATA[postMessage]]></category>
		<category><![CDATA[window.postMessage]]></category>
		<guid isPermaLink="false">http://sibeecst_passion.com/?p=11117</guid>

					<description><![CDATA[[toc] Introduction In this post, we will see how we can send data to another domain from our application. You can simple called it as a 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 postMessage method. The window.postMessage method helps us to enables cross-origin communication. I hope you will like this. Clone Source Code Sender Application Receiver Application Background I was forced to maintain [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>[toc]</p>
<h2>Introduction</h2>
<p>In this post, we will see how we can send data to another domain from our application. You can simple called it as a 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 postMessage method. The window.postMessage method helps us to enables cross-origin communication. I hope you will like this.</p>
<h2><strong>Clone Source Code</strong></h2>
<ul>
<li><a href="https://github.com/SibeeshVenu/Domain-to-Domain-Data-Transfer-Sender-App">Sender Application</a></li>
<li><a href="https://github.com/SibeeshVenu/Domain-to-Domain-Data-Transfer-Receiver-App">Receiver Application</a></li>
</ul>
<h2><strong>Background</strong></h2>
<p>I was forced to maintain some cookies in my second application every time. That too I wanted to set the same for my first application. So I implemented this with the help of window.postMessage. For the demo purpose, here I am creating two application.</p>
<ul>
<li>CrossDomainDataSharing</li>
<li>Receiver</li>
</ul>
<h2><strong>Using the code</strong></h2>
<p>First, we will concentrate on the <em>CrossDomainDataSharing</em> application in which we are sending our data to <em>Receiver </em>application.</p>
<p>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.</p>
<p>[html]<br />
&lt;script src=&#8221;Scripts/jquery-2.2.0.min.js&#8221;&gt;&lt;/script&gt;<br />
&lt;script src=&#8221;js/alert.min.js&#8221;&gt;&lt;/script&gt;<br />
&lt;link href=&#8221;css/alert.min.css&#8221; rel=&#8221;stylesheet&#8221; /&gt;<br />
&lt;link href=&#8221;themes/dark/theme.css&#8221; rel=&#8221;stylesheet&#8221; /&gt;<br />
[/html]</p>
<p>Next thing is &#8220;Calling Window On Load&#8221; Function.</p>
<p>[js]<br />
window.onload = function () {<br />
document.getElementById(&#8216;btnSend&#8217;).addEventListener(&#8216;click&#8217;, sendData);<br />
};<br />
[/js]</p>
<p>Here we are just adding a click event to the button with id <em>btnSend</em>. Before going to add the event, please make sure that you have created a button.</p>
<p>[html]<br />
&lt;button id=&#8221;btnSend&#8221;&gt;Send Feedback&lt;/button&gt;<br />
[/html]</p>
<p>The next thing is to create an iframe and load our second application link to that by giving src value.</p>
<p>[html]<br />
&lt;iframe id=&#8221;ifrLoad&#8221; src=&#8221;http://localhost:55592/Receiver/Cross-Domain-Data-Sharing-Receiver.html&#8221; style=&#8221;display: none;&#8221;&gt;<br />
&lt;p&gt;Oops!. Your browser does not support iframes.&lt;/p&gt;<br />
&lt;/iframe&gt;<br />
[/html]</p>
<p>Have you noticed that we have called a function called <em>sendData</em>. Let us see what that function does.</p>
<p>[js]<br />
function sendData(e) {<br />
try {<br />
$.alert.open(&#8216;prompt&#8217;, &#8216;Please send your feedback!.&#8217;, function (button, value) {<br />
if (button == &#8216;ok&#8217;) {<br />
e.preventDefault();;<br />
var myIfr = window.frames[&#8216;ifrLoad&#8217;].contentWindow;<br />
myIfr.postMessage(value, &#8216;http://localhost:55592/Receiver/Cross-Domain-Data-Sharing-Receiver.html&#8217;)<br />
$.alert.open(&#8216;Thank you so much for your feedback.&#8217; || &#8216;No value has been entered&#8217;);<br />
}<br />
});</p>
<p>} catch (e) {<br />
console.log(&#8216;Error: &#8216; + e.message);<br />
}<br />
}<br />
[/js]</p>
<p>We are creating a smart alert first if the user says &#8216;Ok&#8217;, we will just pass the given feedback to our second application with the help of postMessage method. The implementation is pretty simple. Isn&#8217;t it?</p>
<p>You can style the button you have created as follows.</p>
<p>[css]<br />
&lt;style&gt;<br />
#btnSend {<br />
margin-left: 11px;<br />
border: solid 1px #000;<br />
padding: 9px 22px 7px;<br />
min-width: 32px;<br />
font-weight: bold;<br />
line-height: 13px;<br />
color: #fff;<br />
text-shadow: 0 1px 1px rgba(0, 0, 0, 0.5);<br />
background-color: #232323;<br />
background-image: linear-gradient(#2b2b2b 1%, #1d1d1d 99%);<br />
background-image: -webkit-linear-gradient(#2b2b2b 1%, #1d1d1d 99%);<br />
background-image: -moz-linear-gradient(#2b2b2b 1%, #1d1d1d 99%);<br />
background-image: -o-linear-gradient(#2b2b2b 1%, #1d1d1d 99%);<br />
background-image: -ms-linear-gradient(#2b2b2b 1%, #1d1d1d 99%);<br />
-pie-background: linear-gradient(#2b2b2b 1%, #1d1d1d 99%);<br />
border-radius: 3px;<br />
-webkit-border-radius: 3px;<br />
-moz-border-radius: 3px;<br />
-ms-border-radius: 3px;<br />
box-shadow: 0 1px rgba(2, 3, 3, 0.5), 0 1px rgba(255, 255, 255, 0.07) inset, 0 0 0 1px rgba(255, 255, 255, 0.06) inset;<br />
-webkit-box-shadow: 0 1px rgba(2, 3, 3, 0.5), 0 1px rgba(255, 255, 255, 0.07) inset, 0 0 0 1px rgba(255, 255, 255, 0.06) inset;<br />
-moz-box-shadow: 0 1px rgba(2, 3, 3, 0.5), 0 1px rgba(255, 255, 255, 0.07) inset, 0 0 0 1px rgba(255, 255, 255, 0.06) inset;<br />
-ms-box-shadow: 0 1px rgba(2, 3, 3, 0.5), 0 1px rgba(255, 255, 255, 0.07) inset, 0 0 0 1px rgba(255, 255, 255, 0.06) inset;<br />
-o-box-shadow: 0 1px rgba(2, 3, 3, 0.5), 0 1px rgba(255, 255, 255, 0.07) inset, 0 0 0 1px rgba(255, 255, 255, 0.06) inset;<br />
}</p>
<p>#btnSend:hover {<br />
background-color: #404040;<br />
background-image: linear-gradient(#515151 1%, #2e2e2e);<br />
background-image: -webkit-linear-gradient(#515151 1%, #2e2e2e);<br />
background-image: -moz-linear-gradient(#515151 1%, #2e2e2e);<br />
background-image: -o-linear-gradient(#515151 1%, #2e2e2e);<br />
background-image: -ms-linear-gradient(#515151 1%, #2e2e2e);<br />
-pie-background: linear-gradient(#515151 1%, #2e2e2e);<br />
box-shadow: 0 1px rgba(2, 3, 3, 0.5), 0 1px rgba(255, 255, 255, 0.07) inset, 0 0 0 1px rgba(255, 255, 255, 0.06) inset;<br />
-webkit-box-shadow: 0 1px rgba(2, 3, 3, 0.5), 0 1px rgba(255, 255, 255, 0.07) inset, 0 0 0 1px rgba(255, 255, 255, 0.06) inset;<br />
-moz-box-shadow: 0 1px rgba(2, 3, 3, 0.5), 0 1px rgba(255, 255, 255, 0.07) inset, 0 0 0 1px rgba(255, 255, 255, 0.06) inset;<br />
-ms-box-shadow: 0 1px rgba(2, 3, 3, 0.5), 0 1px rgba(255, 255, 255, 0.07) inset, 0 0 0 1px rgba(255, 255, 255, 0.06) inset;<br />
-o-box-shadow: 0 1px rgba(2, 3, 3, 0.5), 0 1px rgba(255, 255, 255, 0.07) inset, 0 0 0 1px rgba(255, 255, 255, 0.06) inset;<br />
}<br />
&lt;/style&gt;<br />
[/css]</p>
<p>Now that is all for the sending part, please see the complete code for our first application here.</p>
<p>[html]<br />
&lt;!DOCTYPE html&gt;<br />
&lt;html&gt;<br />
&lt;head&gt;<br />
&lt;title&gt;Cross Domain Data Sharing Demo &#8211; Sibeesh Passion&lt;/title&gt;<br />
&lt;script src=&#8221;Scripts/jquery-2.2.0.min.js&#8221;&gt;&lt;/script&gt;<br />
&lt;script src=&#8221;js/alert.min.js&#8221;&gt;&lt;/script&gt;<br />
&lt;link href=&#8221;css/alert.min.css&#8221; rel=&#8221;stylesheet&#8221; /&gt;<br />
&lt;link href=&#8221;themes/dark/theme.css&#8221; rel=&#8221;stylesheet&#8221; /&gt;<br />
&lt;style&gt;<br />
#btnSend {<br />
margin-left: 11px;<br />
border: solid 1px #000;<br />
padding: 9px 22px 7px;<br />
min-width: 32px;<br />
font-weight: bold;<br />
line-height: 13px;<br />
color: #fff;<br />
text-shadow: 0 1px 1px rgba(0, 0, 0, 0.5);<br />
background-color: #232323;<br />
background-image: linear-gradient(#2b2b2b 1%, #1d1d1d 99%);<br />
background-image: -webkit-linear-gradient(#2b2b2b 1%, #1d1d1d 99%);<br />
background-image: -moz-linear-gradient(#2b2b2b 1%, #1d1d1d 99%);<br />
background-image: -o-linear-gradient(#2b2b2b 1%, #1d1d1d 99%);<br />
background-image: -ms-linear-gradient(#2b2b2b 1%, #1d1d1d 99%);<br />
-pie-background: linear-gradient(#2b2b2b 1%, #1d1d1d 99%);<br />
border-radius: 3px;<br />
-webkit-border-radius: 3px;<br />
-moz-border-radius: 3px;<br />
-ms-border-radius: 3px;<br />
box-shadow: 0 1px rgba(2, 3, 3, 0.5), 0 1px rgba(255, 255, 255, 0.07) inset, 0 0 0 1px rgba(255, 255, 255, 0.06) inset;<br />
-webkit-box-shadow: 0 1px rgba(2, 3, 3, 0.5), 0 1px rgba(255, 255, 255, 0.07) inset, 0 0 0 1px rgba(255, 255, 255, 0.06) inset;<br />
-moz-box-shadow: 0 1px rgba(2, 3, 3, 0.5), 0 1px rgba(255, 255, 255, 0.07) inset, 0 0 0 1px rgba(255, 255, 255, 0.06) inset;<br />
-ms-box-shadow: 0 1px rgba(2, 3, 3, 0.5), 0 1px rgba(255, 255, 255, 0.07) inset, 0 0 0 1px rgba(255, 255, 255, 0.06) inset;<br />
-o-box-shadow: 0 1px rgba(2, 3, 3, 0.5), 0 1px rgba(255, 255, 255, 0.07) inset, 0 0 0 1px rgba(255, 255, 255, 0.06) inset;<br />
}</p>
<p>#btnSend:hover {<br />
background-color: #404040;<br />
background-image: linear-gradient(#515151 1%, #2e2e2e);<br />
background-image: -webkit-linear-gradient(#515151 1%, #2e2e2e);<br />
background-image: -moz-linear-gradient(#515151 1%, #2e2e2e);<br />
background-image: -o-linear-gradient(#515151 1%, #2e2e2e);<br />
background-image: -ms-linear-gradient(#515151 1%, #2e2e2e);<br />
-pie-background: linear-gradient(#515151 1%, #2e2e2e);<br />
box-shadow: 0 1px rgba(2, 3, 3, 0.5), 0 1px rgba(255, 255, 255, 0.07) inset, 0 0 0 1px rgba(255, 255, 255, 0.06) inset;<br />
-webkit-box-shadow: 0 1px rgba(2, 3, 3, 0.5), 0 1px rgba(255, 255, 255, 0.07) inset, 0 0 0 1px rgba(255, 255, 255, 0.06) inset;<br />
-moz-box-shadow: 0 1px rgba(2, 3, 3, 0.5), 0 1px rgba(255, 255, 255, 0.07) inset, 0 0 0 1px rgba(255, 255, 255, 0.06) inset;<br />
-ms-box-shadow: 0 1px rgba(2, 3, 3, 0.5), 0 1px rgba(255, 255, 255, 0.07) inset, 0 0 0 1px rgba(255, 255, 255, 0.06) inset;<br />
-o-box-shadow: 0 1px rgba(2, 3, 3, 0.5), 0 1px rgba(255, 255, 255, 0.07) inset, 0 0 0 1px rgba(255, 255, 255, 0.06) inset;<br />
}<br />
&lt;/style&gt;<br />
&lt;script&gt;<br />
window.onload = function () {<br />
document.getElementById(&#8216;btnSend&#8217;).addEventListener(&#8216;click&#8217;, sendData);<br />
};<br />
function sendData(e) {<br />
try {<br />
$.alert.open(&#8216;prompt&#8217;, &#8216;Please send your feedback!.&#8217;, function (button, value) {<br />
if (button == &#8216;ok&#8217;) {<br />
e.preventDefault();;<br />
var myIfr = window.frames[&#8216;ifrLoad&#8217;].contentWindow;<br />
myIfr.postMessage(value, &#8216;http://localhost:55592/Receiver/Cross-Domain-Data-Sharing-Receiver.html&#8217;)<br />
$.alert.open(&#8216;Thank you so much for your feedback.&#8217; || &#8216;No value has been entered&#8217;);<br />
}<br />
});</p>
<p>} catch (e) {<br />
console.log(&#8216;Error: &#8216; + e.message);<br />
}<br />
}<br />
&lt;/script&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
&lt;button id=&#8221;btnSend&#8221;&gt;Send Feedback&lt;/button&gt;<br />
&lt;iframe id=&#8221;ifrLoad&#8221; src=&#8221;http://localhost:55592/Receiver/Cross-Domain-Data-Sharing-Receiver.html&#8221; style=&#8221;display: none;&#8221;&gt;<br />
&lt;p&gt;Oops!. Your browser does not support iframes.&lt;/p&gt;<br />
&lt;/iframe&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;</p>
<p>[/html]</p>
<p>Now we will concentrate on our second application &#8216;<em>Receiver</em>&#8216;</p>
<p>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 furtehr clarification.</p>
<p>[js]<br />
$(window).load(function () {<br />
window.addEventListener(&#8216;message&#8217;, addData);<br />
function addData(e) {<br />
document.cookie = &#8220;myData=&#8221; + e.data;<br />
};<br />
});<br />
[/js]</p>
<blockquote><p>Here myData is the cookie name.</p></blockquote>
<p>You can see the complete code for our second application here.</p>
<p>[html]<br />
&lt;!DOCTYPE html&gt;<br />
&lt;html&gt;<br />
&lt;head&gt;<br />
&lt;title&gt;Cross Domain Data Sharing Receiver &#8211; Sibeesh Passion&lt;/title&gt;<br />
&lt;script src=&#8221;Scripts/jquery-2.2.0.min.js&#8221;&gt;&lt;/script&gt;<br />
&lt;script&gt;<br />
$(window).load(function () {<br />
window.addEventListener(&#8216;message&#8217;, addData);<br />
function addData(e) {<br />
document.cookie = &#8220;myData=&#8221; + e.data;<br />
};<br />
});<br />
&lt;/script&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;<br />
[/html]</p>
<p>Now it is time to see our output.</p>
<p><strong>Output</strong></p>
<p>While sending the data to our second application.</p>
<div id="attachment_11126" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/01/Cross_Domain_Data_Sharing_Sender.png"><img fetchpriority="high" decoding="async" aria-describedby="caption-attachment-11126" class="size-large wp-image-11126" src="http://sibeeshpassion.com/wp-content/uploads/2016/01/Cross_Domain_Data_Sharing_Sender-1024x538.png" alt="Cross_Domain_Data_Sharing_Sender" width="634" height="333" srcset="/wp-content/uploads/2016/01/Cross_Domain_Data_Sharing_Sender-1024x538.png 1024w, /wp-content/uploads/2016/01/Cross_Domain_Data_Sharing_Sender-300x158.png 300w, /wp-content/uploads/2016/01/Cross_Domain_Data_Sharing_Sender-768x404.png 768w, /wp-content/uploads/2016/01/Cross_Domain_Data_Sharing_Sender-600x315.png 600w, /wp-content/uploads/2016/01/Cross_Domain_Data_Sharing_Sender-400x210.png 400w, /wp-content/uploads/2016/01/Cross_Domain_Data_Sharing_Sender-1142x600.png 1142w, /wp-content/uploads/2016/01/Cross_Domain_Data_Sharing_Sender.png 1298w" sizes="(max-width: 634px) 100vw, 634px" /></a><p id="caption-attachment-11126" class="wp-caption-text">Cross_Domain_Data_Sharing_Sender</p></div>
<div id="attachment_11127" style="width: 523px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/01/Smart_Alert.png"><img decoding="async" aria-describedby="caption-attachment-11127" class="size-full wp-image-11127" src="http://sibeeshpassion.com/wp-content/uploads/2016/01/Smart_Alert.png" alt="Smart_Alert" width="513" height="307" srcset="/wp-content/uploads/2016/01/Smart_Alert.png 513w, /wp-content/uploads/2016/01/Smart_Alert-300x180.png 300w, /wp-content/uploads/2016/01/Smart_Alert-400x239.png 400w" sizes="(max-width: 513px) 100vw, 513px" /></a><p id="caption-attachment-11127" class="wp-caption-text">Smart_Alert</p></div>
<p>While receiving the data in our second application.</p>
<div id="attachment_11128" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/01/Receiving_data_from_another_domain.png"><img decoding="async" aria-describedby="caption-attachment-11128" class="size-large wp-image-11128" src="http://sibeeshpassion.com/wp-content/uploads/2016/01/Receiving_data_from_another_domain-1024x540.png" alt="Receiving_data_from_another_domain" width="634" height="334" srcset="/wp-content/uploads/2016/01/Receiving_data_from_another_domain-1024x540.png 1024w, /wp-content/uploads/2016/01/Receiving_data_from_another_domain-300x158.png 300w, /wp-content/uploads/2016/01/Receiving_data_from_another_domain-768x405.png 768w, /wp-content/uploads/2016/01/Receiving_data_from_another_domain-600x315.png 600w, /wp-content/uploads/2016/01/Receiving_data_from_another_domain-400x211.png 400w, /wp-content/uploads/2016/01/Receiving_data_from_another_domain-1137x600.png 1137w, /wp-content/uploads/2016/01/Receiving_data_from_another_domain.png 1463w" sizes="(max-width: 634px) 100vw, 634px" /></a><p id="caption-attachment-11128" class="wp-caption-text">Receiving_data_from_another_domain</p></div>
<p>That is all. We did it. Have a happy coding.</p>
<h2><strong>Conclusion</strong></h2>
<p>Did I miss anything that you may think which is needed? Have you ever wanted to do this requirement? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.</p>
<h2><strong>Your turn. What do you think?</strong></h2>
<p>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.</p>
<p>Kindest Regards<br />
Sibeesh Venu</p>
]]></content:encoded>
					
					<wfw:commentRss>https://mail.sibeeshpassion.com/sending-data-to-another-domain-usign-postmessage/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>
