Created
February 25, 2016 18:58
-
-
Save zxqx/1302cd91f033a0e56848 to your computer and use it in GitHub Desktop.
Compile a collection of stringified LESS styles directly in the browser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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