this is definitely possible, but I had to change the source code a bit (which I think is fine, given that it really is not meant to be executed :))
I assume that everyone wants to see the demo first, click here: http://jsfiddle.net/kritzikratzi/BRJXU/1/ (the script-winindow contains only the modified less.js source, all that is interesting in the html window)
kk, I will first explain my patch, use at the end.
Patch
consists of three parts
add utility function
less.Overrides = {}; less.Override = function( variableName, value ){ if( !value ){ delete less.Overrides[variableName]; } else{ less.Overrides[variableName] = value; } less.refreshStyles(); };
save a property in an object and say less to update its styles.
change parsing function
function parse(str, callback ){ ... var overrides = "\n\n"; for( var key in less.Overrides ){ overrides += key + ": " + less.Overrides[key] + ";\n"; } str += overrides;
all we do is serialize the overridden properties and add them to the end of each processed file.
change loadStyles function
if (styles[i].type.match(typePattern) || styles[i].hasAttribute( "lessText" )) { var lessText; if( styles[i].hasAttribute( "lessText" ) ){ lessText = styles[i].getAttribute( "lessText" ); } else{ lessText = styles[i].innerHTML || ''; styles[i].setAttribute( "lessText", lessText ); } ....
by default it will replace the type parameter less from <style type='text/less'> to type='text/css' and forget about the less source. to prevent this, the original less source is loaded and loaded.
use and conclusion
<style type="text/less"> @color: green; #header{ color: @color; } </style> <div id="header">i'm the header!</div> <a href="#" onclick="less.Override('@color', 'red');">make it red</a>
this works fine on my computer, and I have to admit that it looks very neat. I have not tested smaller external files, if they do not work, they should be easy to fix.
I still think that it is not a good idea to use this in a production environment (for reasons mentioned by others already).