Last active
September 29, 2015 21:08
-
-
Save dangerouse/1668726 to your computer and use it in GitHub Desktop.
Cloudsponge Widget Reference - Launch widget from a cross domain iframe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE HTML> | |
<html> | |
<head> | |
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> | |
<script type="text/javascript" charset="utf-8"> | |
var parent_domain = "http://parent_domain.com"; | |
function launchCloudspongeInParent() { | |
window.parent.postMessage("launch", parent_domain); | |
return false; | |
} | |
$(window).on("message", function(jEvent) { | |
var text_area; | |
var event = jEvent.originalEvent; | |
// check to be sure the message is from the correct window | |
if (event.origin === parent_domain && event.source === window.parent) { | |
// populate the text area with the data we got | |
text_area = document.getElementById('contact_list'); | |
text_area.value = event.data; | |
} | |
}); | |
</script> | |
<body> | |
<h2>IFrame Import Child Page</h2> | |
<!-- | |
Create a custom link to start the import process | |
--> | |
<a id="cs_import_link" href="#" onclick="return launchCloudspongeInParent();">Add from Address Book</a> | |
<!-- This textarea will be populated with the contacts returned by CloudSponge --> | |
<textarea id="contact_list" style="width:450px;height:82px"></textarea> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE HTML> | |
<html> | |
<head> | |
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> | |
</head> | |
<body> | |
<h2>IFrame Import Parent Page</h2> | |
<!-- The iframe that is on a different domain to this page --> | |
<iframe id="ab" width="800" height="600" src="http://child_domain.com/child.html"></iframe> | |
<script type="text/javascript"> | |
// Asynchronously include the widget library. | |
// TODO: replace with your widget script | |
(function(u){ | |
var d=document,s='script',a=d.createElement(s),m=d.getElementsByTagName(s)[0]; | |
a.async=1;a.src=u;m.parentNode.insertBefore(a,m); | |
})('//api.cloudsponge.com/widget/YOUR_WIDGET_SCRIPT.js'); | |
var child_domain = "http://parent_domain.com" | |
// The widget initialization needs to be on the parent frame so | |
// that the widget can overlay the entire page | |
window.csPageOptions = { | |
// this callback handles converting the array of contacts retuned into a | |
// comma separated list of email addresses. | |
afterSubmitContacts:function(array_of_contacts) { | |
var contacts_display_string = [], | |
contact = null; | |
for (var i = 0; i < array_of_contacts.length; i++) { | |
contact = array_of_contacts[i]; | |
contacts_display_string.push(contact.primaryEmail()); | |
} | |
// post a message to the child frame, passing the string to populate | |
// in the text area | |
window.frames["ab"].postMessage(contacts_display_string.join(', '), child_domain); | |
} | |
}; | |
$(window).on("message", function(jEvent) { | |
var event = jEvent.originalEvent; | |
// accept the message only if it is from our child | |
// this test is important because the cloudsponge widget | |
// will attempt to send several messages to this frame | |
if (event.origin === child_domain && event.source === window.frames["ab"] && event.data === "launch") { | |
cloudsponge.launch(); | |
} | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment