Skip to content

Instantly share code, notes, and snippets.

@greglinch
Last active December 15, 2015 15:41
Show Gist options
  • Save greglinch/bf259910d74314e16cf1 to your computer and use it in GitHub Desktop.
Save greglinch/bf259910d74314e16cf1 to your computer and use it in GitHub Desktop.
Tired of verbose code embeds? Insert HTML, CSS or JS onto a page using one line per code block. Also handy when sharing across sites: you just update the main JS file and don't need to re-send updated embed codes. NOTE: I'd recommend renaming the variables and IDs to things that relate to the content so it's better self-documenting code.
//// JAVASCRIPT ////
// Set the vars
// be sure to minify the code: http://www.willpeavy.com/minifier
var cssContentOne = 'CSS HERE';
var htmlContentOne = 'HTML HERE';
var htmlContentTwo = 'HTML HERE';
// first item
var cssOne = document.createElement('style');
cssOne.innerHTML= cssContentOne;
var myCSSitem = document.getElementById('cssIdOne');
myCSSitem.parentNode.insertBefore(cssOne, myCSSitem);
// second item
var htmlOne = document.createElement('span');
htmlOne.innerHTML = htmlContentOne;
var myHTMLOne = document.getElementById('spanIdOne');
myHTMLOne.parentNode.insertBefore(htmlOne, myHTMLOne);
// third item
var htmlTwo = document.createElement('span');
hmtlTwo.innerHTML = htmlContentTwo;
var myHTMLScriptTwo=document.getElementById('spanIdTwo');
myHTMLTwo.parentNode.insertBefore(htmlTwo, myHTMLTwo);
// etc, etc. Copy-paste and modify to include more.
//// HTML ////
// Insert these where you'd otherwise embed the code directly.
// Use at least one that corresponds to the script you modify.
/*
<span id="cssIdOne"></span>
<span id="spanIdOne"></span>
<span id="spanIdTwo"></span>
// Required
<script src="http://FULL/PATH/TO/FILE/add-to-page.js"></script>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment