Search code examples
javascriptgoogle-chromefirefoxdom-eventsyui

YUI3 events not working in Firefox or Opera, works fine in Chrome though


I have a very simple YUI3 script which changes background of a web page. Here is the source:

<html>
<head>
<title>YUI 3 events do not work!</title>
</head>
<body id="doc-body">
<div id="rgbvalues"> </div>
<script src="http://yui.yahooapis.com/3.3.0/build/yui/yui-min.js" charset="utf-8"></script>
<script type="text/javascript">
//use node and event modules
YUI().use("node", "event", function(Y){
    var handleClick = function(e) {
        var docBody = Y.one("#doc-body"); //equivalent to $(element) in prototype and jQuery(element)
        var winHeight = docBody.get('winHeight');//get "viewport" height
        var winWidth = docBody.get('winWidth');//get "viewport" width
        var red = parseInt (e.pageX * (255/winWidth));
        var green = parseInt(e.pageY * (255/winHeight));
        var blue = parseInt (red*green*0.004);
        var bgstyle = "rgb("+red+","+green+"," +blue+")";
        console.log("setting background-color:"+bgstyle+" for #doc-body");
        docBody.setStyle("background-color", bgstyle);//same as $(element).setStyle() from prototype and jQuery(element).css()
        Y.one("#rgbvalues").set("innerHTML", bgstyle);//set innerHTML
    };
    Y.on("mousemove", handleClick, "#doc-body");
});
</script>
</body>
</html>

The script doesn't work in Firefox 5 or Opera but works well in Chrome. There is no error on Firebug console either. Even if I change the event from mousemove to click it doesn't work.


Solution

  • To fix your problem, add this CSS:

    html, body {
        margin: 0;
        padding: 0;
        height: 100%
    }
    

    The html and body elements do not take up 100% height by default.

    The reason for the inconsistent behaviour between browsers is probably to do with the fact that your page is using Quirks Mode, because you don't have a doctype. Add this as the very first line:

    <!DOCTYPE html>