const iframe = document.getElementById('showcase');
const jsFiddleKey = "a8a9d13f72124cf6865af11c42a843a1";
const version = "3.5";
// serialize the contents of the function and call it with a single parameter.
// Then, wrap it with a script tag. '<' is separated from '/' to prevent the browser from
// adding this script tag to the parent page on jsfiddle.
function serializeFunctionCall(myFunction, mattertagId) {
return (
`<script>
${myFunction.toString()};
${myFunction.name}('${mattertagId}');
<` + `/script>`);
}
// The contents of this function will be executed in a sandboxed mattertag iframe.
// You can only refer to variables defined in the iframe.
function mattertagFunction(mattertagId) {
// when the button is clicked send a message back to the parent page through the window object.
var btn1 = document.getElementById('btn1');
btn1.addEventListener('click', function () {
window.send('click', {
id: mattertagId
});
});
// handle a message that is originated from the parent page.
window.on('update.button', function (color, message) {
btn1.innerText = message;
btn1.style.backgroundColor = color;
});
};
// style and script tags are added to the head section.
// all other elements are added to the body section.
const html = `
<style>
body {
background-color: #cccccc;
margin: 0px;
}
div {
height: 36px;
background-color: rgb(255, 255, 255);
border-radius: 10px;
border: 1px solid black;
text-align: center;
margin: 5px;
}
div:hover {
background-color: rgb(235, 235, 235);
}
div:active {
background-color: rgb(200, 200, 200);
}
</style>
<div id="btn1">PURCHASE</div>
`;
function makeHtmlString(mattertagId) {
return html + serializeFunctionCall(mattertagFunction, mattertagId);
}
const main = async function() {
const sdk = await window.MP_SDK.connect(iframe, jsFiddleKey, version);
console.log('SDK Connected', sdk);
await...
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.