ThinkGeo.com    |     Documentation    |     Premium Support

"This.element is null or not an object" Error

I am getting a javascript error, "this.element.scrolls is null or not an object", whenever I disabled the navigation control. This is only happening on IE 7, not Firefox. Here are the steps how this error is generated.


Zoom to an map location --> click on a button to disable/destroy the navigation's ZoomWheel (see below code) --> Click on the map and scroll the wheel up and down.


I tried to handle the mouse wheel error by using the window's event listerner without any success.                                                   window.addEventListener('DOMMouseScroll', wheel, false);.


JAVASCRIPT:                                                                                                                                                                                        function disableNav() {                                                                                                                                                                                                       


var navigation = map.getControlsByClass("OpenLayers.Control.Navigation")[0];

 if (navigation != null) {

         navigation.disableZoomWheel();

         navigation.dragPan.deactivate();

         navigation.dragPan.wheelListener = null;                     

        }

        var ZoomBar = map.getControlsByClass("OpenLayers.Control.PanZoomBar")[0];

 if (ZoomBar != null) {

         ZoomBar.destroy();

         wheelListener = null;

 } }



Todd, can you try window.attachEvent(‘eventType’, eventListener) in IE instead?  
  
 Ben

In order to duplicate this problem, make the map’s height 800-1000 px. The browser must contain scrollbar. Again, this only occurs in IE browser. Wonder if you have a workaround for this?

Ttd,


This is a known issue in OpenLayers library, please see trac.openlayers.org/ticket/1952 for more information. It can be worked around by the following scripts:



     function OnMapCreating() {
            OpenLayers.Events.prototype.includeXY = true;
            OpenLayers.Events.prototype.getMousePosition = function (evt) {
                    if (!this.includeXY) {
                        this.clearMouseCache();
                    } else if (!this.element.hasScrollEvent) {
                        OpenLayers.Event.observe(window, 'scroll',
                            OpenLayers.Function.bind(this.clearMouseCache, this));
                        this.element.hasScrollEvent = true;
                    }
                   
                    if (!this.element.scrolls) {
                          this.element.scrolls = [ 
                                     (document.documentElement.scrollLeft 
                                              || document.body.scrollLeft), 
                                     (document.documentElement.scrollTop 
                                              || document.body.scrollTop) 
                                 ];
                    }
 
                    if (!this.element.lefttop) {
                          this.element.lefttop = [ 
                                       (document.documentElement.clientLeft || 0), 
                                       (document.documentElement.clientTop  || 0) 
                                   ]; 
                    }
                   
                    if (!this.element.offsets) {
                        this.element.offsets = OpenLayers.Util.pagePosition(this.element);
                        this.element.offsets[0] += this.element.scrolls[0];
                        this.element.offsets[1] += this.element.scrolls[1];
                    }
 
                    return new OpenLayers.Pixel(
                        (evt.clientX + this.element.scrolls[0]) - this.element.offsets[0]
                                     - this.element.lefttop[0],
                        (evt.clientY + this.element.scrolls[1]) - this.element.offsets[1]
                                     - this.element.lefttop[1]
                    );
                }
        }

Thanks, 


Ben



This code seems to resolve most of the issue. However, whenever I set Map1.SetDrawMode(‘Normal’); I can always generate the error. Thanks

Ttd, 
  
 It should be solved by adding the following script to the above OnMapCreating function. 
  
 
        OpenLayers.Events.prototype.clearMouseCache = function() {
                if (this.element) {`
                    this.element.scrolls = null;
                    this.element.lefttop = null;
                    this.element.offsets = null;
                }
            };
 
  
 Thanks, 
  
 Ben