<input onclick="color();" value="Жми" type="button">
<script type="text/javascript">
function toHexColor( color ) {
return color.replace( /rgba?\(([^\)]+)\)|#([a-f0-9]{6}|[a-f0-9]{3})/ig, function( m, rgb, hex ) {
if ( rgb = /([0-9\.]+)(\%?)\s*,\s*([0-9\.]+)(\%?)\s*,\s*([0-9\.]+)(\%?)\s*(?:,\s*([0-9\.]+))?/g.exec( rgb || "" ) ) {
hex = parseInt( rgb[2] ? rgb[1] * 2.55 : rgb[1] ).toString( 16 ).replace( /^(.)$/, '0$1' ) +
parseInt( rgb[4] ? rgb[3] * 2.55 : rgb[3] ).toString( 16 ).replace( /^(.)$/, '0$1' ) +
parseInt( rgb[6] ? rgb[5] * 2.55 : rgb[5] ).toString( 16 ).replace( /^(.)$/, '0$1' );
m = rgb[ 7 ] == "" || rgb[ 7 ] === undefined ? 1 : parseFloat( rgb[ 7 ] );
} else if ( hex && ( m = 1 ) ) {
hex = hex.replace( /^(.)(.)(.)$/, "$1$1$2$2$3$3" );
} else {
return m;
}
return "#" + hex;
});
}
function color() {
if ( toHexColor( document.body.style.backgroundColor ) == '')
return document.body.style.backgroundColor='#f00';
if ( toHexColor( document.body.style.backgroundColor ) == '#ff0000')
return document.body.style.backgroundColor='#0f0';
if ( toHexColor( document.body.style.backgroundColor ) == '#00ff00')
return document.body.style.backgroundColor='#f00';
}
</script>