views:

20

answers:

1

I need to create a shim for the entire page to capture click events, but the shim must not stop other events like mouseover etc on the other html elements on the page.

I have solution that sort of works, but I am having a couple of problems with it: Whenever I click on the page, if I click directly on some text, the click event of the shim does not fire (there is no event-cancelling on the page).

The onclick problem only occurs in IE.

In FF and Chrome there is a problem with the mouseover events not being fired in the background, so I would of course appreciate any tips on solving that as well...

Here is a very basic example demonstrating the problem.

Any ideas? :-)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html>
<head>
    <style type="text/css">
        body
        {
            font-size:20px;
            height:100%;
        }
        #shim
        {
            position: absolute;
            padding: 0;
            margin: 0;
            top: 0px;
            left: 0px;
            width: 100%;
            height: 100%;
            zindex: 1;
        }
        #trace
        {
            border: 1px solid #ccc;
            height: 100px;
        }
    </style>

    <script type="text/javascript">
        function trace(e) {
            e = e || event;
            document.getElementById('trace').innerHTML = "Hovered innerHTML: " + (e.target || e.srcElement).innerHTML;
        }
    </script>

</head>
<body>
    <div id="shim" onclick="alert('shim clicked')">
    </div>
    <table border="1" onmouseover="trace(event)">
        <tr>
            <td>
                AAAAAA AAAAAA
            </td>
            <td>
                BBBBBB BBBBB
            </td>
            <td>
                CCCCCCCC CCCCC
            </td>
            <td>
                DDDDDD DDDDDDDDDD
            </td>
            <td>
                EEEEEE EEEE
            </td>
            <td>
                FFFFFF FFFFF
            </td>
            <td>
                GGGG GGGGGGGG
            </td>
            <td>
                EEEE EEEE
            </td>
            <td>
                FFFF FFFFFF
            </td>
        </tr>
    </table>
    <div id="trace">
    </div>
</body>
</html>
A: 

I do not know if it is possible to fix your problem, but I have done something like a few weeks ago that works for me.

Basically, I create a global event handler, registering all events I need in the document element and then, using event.srcElement or event.element, I was able to get the element that fires the event in the first place. Based on the event and the element, I can decide what to do.

Jr. Hames