Skip to content

Instantly share code, notes, and snippets.

@tomhodgins
Last active August 29, 2015 14:15
Show Gist options
  • Save tomhodgins/1cd7ec5b0d6300aa03e4 to your computer and use it in GitHub Desktop.
Save tomhodgins/1cd7ec5b0d6300aa03e4 to your computer and use it in GitHub Desktop.
Self Executing JavaScript Widget (HTML + CSS): just place a link to this script in your HTML where you want the content to appear, like <script src="myWidget.js"></script>
(function() {
var myWidget = document.createElement('section'),
widgetStyles = document.createElement('style'),
tag = document.querySelectorAll('[src*="myWidget"]')[0];
myWidget.innerHTML = '\
<!-- Add your HTML here, with line-breaks escaped by a "\" backslash character. -->\
<!-- (Don\'t forget: you can escape any character, like quotes, as well -->\
Hello\
';
widgetStyles.innerHTML = '\
/* Add your CSS here, with line-breaks escaped by a "\" backslash character */\
html { background: #09f; }\
';
function populateWidget(content) {
tag.parentNode.insertBefore(content, tag.nextSibling);
};
populateWidget(widgetStyles);
populateWidget(myWidget);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment