Skip to content

Instantly share code, notes, and snippets.

@zxqx
Created February 25, 2016 18:58
Show Gist options
  • Save zxqx/1302cd91f033a0e56848 to your computer and use it in GitHub Desktop.
Save zxqx/1302cd91f033a0e56848 to your computer and use it in GitHub Desktop.
Compile a collection of stringified LESS styles directly in the browser
/**
* Compile a collection of stringified LESS styles directly in the browser
* @param {string} less
*
* e.g. compileLess('body { div { border: 2px solid aqua; } }');
*/
function compileLess(less)
{
var style = document.createElement('style');
style.type = 'text/less';
style.innerHTML = less;
var lessScript = document.createElement('script');
lessScript.src = 'https://cdnjs.cloudflare.com/ajax/libs/less.js/2.6.0/less.min.js';
document.body.appendChild(style);
document.body.appendChild(lessScript);
// clear out weird script that gets added after less script runs
setTimeout(function() {
var headScripts = document.head.querySelectorAll('style');
headScripts[headScripts.length - 1].remove();
}, 1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment