Friday, April 3, 2009

Change HTML background colour

More External Interface stuff. This snippet of code will enable you to change the background colour of a HTML page. Add the following Actionscript and a movieclip with an instance name of "btn":
import flash.external.*;
var jsCall:String;
btn.onPress = function() {
jsCall = String(ExternalInterface.call("changeBgColor", "#fffecb));
};

Add the following Javascript to your HTML code:

<script language="JavaScript">
function changeBgColor(newBgColor)
{
if (window.document && window.document.bgColor)
{
document.bgColor = newBgColor;
}
}
</script>

You can add a little random spice by using the following Actionscript:
import flash.external.*;
var jsCall:String;
btn.onPress = function() {
ranColor = Math.round(Math.random() * 0xFFFFFF);
jsCall = String(ExternalInterface.call("changeBgColor", ranColor));
};

You can see a working example here along with the code.
Follow this link for details on how to change the background image.