//This function is called whenever the window receives a message event, the message object is passed in as it's only parameter
function receiver(message) {
console.log('receiver')
//get the message container html element (in this case, the messages div)
var messagecontainer = document.getElementById("messages");
var trusteddomain = "http://jsfiddle.net";
//Get the time of message receipt
var currenttime = new Date();
//format the time into a user readable format
var formattedtime = currenttime.getHours() + ":" + currenttime.getMinutes() + ":" + currenttime.getSeconds();
//inspect the origin property of the message event to ensure the message originates from the same domain)
if (message.origin == trusteddomain) {
var msgcontent = message.data;
//check the content of the message only contains letters and numbers to prevent xss attacks
if (msgcontent.match(/^[A-Za-z0-9]+$/)) {
//if no illegal characters are found in the message, print it to the message container div along with the time of receipt
messagecontainer.innerHTML += "message received @ " + formattedtime + ": " + message.data + "<br />";
} else {
//if illegal characters are found in the message content, print an error message to the message container
messagecontainer.innerHTML += "Illegal characters found in the message received @ " + formattedtime + ". Message rejected<br/>";
}
} else {
messagecontainer.innerHTML += "Message received from un-trusted domain:" + message.origin + "<br />";
}
}
//Add an event listener to the window object to catch any message events
window.addEventListener('message', receiver, false);
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.