Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird Javascript error in IE

Getting a weird error for .stopPropagation() in IE

My code is as follows:

$(document).ready(function(){
    var options = {
    $this: "",
    $menuItems: $(".mainMenu > li"),
    $blueBar: $(".blueBar"),
    $submenuBg: $("#submenuBg"),
    $sortOptions: $(".sortOptions"),
    $submenu: $(".submenu"),
    submenuClass: ".submenu",
    blueBarClass: ".blueBar",
    selectedClass: "selected",
    sortOptionsClass: ".sortOptions",
    subSubmenu: "ul",
    subSubmenuClass: "sub-submenu"      
    };

    $sortBy.toggle(function(){
            options.$this = $(this);
            ddlShow(event, options);
        }, 
        function(){
            options.$this = $(this);
            ddlHide(options);
        }
    );

});

var ddlShow = function(event, options){
    event.stopPropagation();
    options.$this.children(options.sortOptionsClass).show();    
}

var ddlHide = function(options){
options.$this.children(options.sortOptionsClass).hide();
}

Getting the following error:

object doesn't support property or method 'stoppropagation'

Code works fine in Chrome and Firefox.

How do I solve this?

Note: The same code works fine without the object options.

like image 523
painotpi Avatar asked Aug 01 '12 10:08

painotpi


4 Answers

in IE we do not have stopPropogation method available in javascript on event object. If you will search in google you will find different implementation of event object in ie and webkit based browsers. So in order to correct that jQuery has given a single interface for handling it so you should use jQuery way of doing events to stop getting this errors.

You can read this article to get more clarification http://www.quirksmode.org/js/introevents.html

like image 155
sushil bharwani Avatar answered Nov 04 '22 02:11

sushil bharwani


replace e.stopPropagation(); with if (e.stopPropagation) e.stopPropagation(); else e.cancelBubble = true; worked for me

Thanks

like image 32
Santosh Avatar answered Nov 04 '22 00:11

Santosh


Have your toggle handlers accept the event parameter from jQuery:

$sortBy.toggle(function(event){ // ADDED PARAMETER
        options.$this = $(this);
        ddlShow(event, options);
    }, 

If you do not do this, then when calling ddlShow the argument event resolves to window.event, which is an object that has not been "normalized" by jQuery for cross-browser consistency.

like image 3
Jon Avatar answered Nov 04 '22 02:11

Jon


Here:

$sortBy.toggle(function(){  <-- you must get the event object from jquery.
        options.$this = $(this);
        ddlShow(event, options);
    }, 
    function(){
        options.$this = $(this);
        ddlHide(options);
    }
);

Then:

$sortBy.toggle(function(event){ ... ddlShow(event, options); }...
like image 1
Marcelo De Zen Avatar answered Nov 04 '22 01:11

Marcelo De Zen