culter
culter

Reputation: 5697

JavaScript error "Function is expected"

I have several listeners like this, which listen to click and then displays content within <div id="c50"><a hre...>CONTENT</a></div> (in this case). Everything works in Opera, Chrome and FF, but not in IE.

google.maps.event.addListener(pano50, 'click', function() {    
fireEvent(document.getElementById("c50").getElementsByTagName("a")[0], 'click');
})

Chrome javascript console tool displays this error after click (but works fine):

Uncaught TypeError: object is not a function

but traditionally, IE8 displays:

Function expected on line 817

which is the first line of code above and do nothing after click. Thank you for any advice!

EDIT: here is the fireEvent function:

function fireEvent(element, event) {
    if (document.createEventObject){
        /* for IE */
        return element.fireEvent('on' + event, document.createEventObject());
    }else{
        /* for other browsers */
        var evt = document.createEvent('HTMLEvents');
        evt.initEvent(event, true, true);
    }
    return !element.dispatchEvent(evt);
}

Upvotes: 2

Views: 4701

Answers (2)

gilly3
gilly3

Reputation: 91467

You've got MooTools running on your page. MooTools overrides IE's built-in element.fireEvent() method with its own normalized method that works for all browsers. MooTools' version of fireEvent() expects "click" instead of "onclick".

You can fix this one issue by simply changing your fireEvent function to use "click" instead of "onclick":

/* for IE */
return element.fireEvent(event, document.createEventObject());

But, since MooTools normalizes element.fireEvent to work with all browsers, you may be able to ditch your fireEvent function, and instead just call element.fireEvent() directly.

You may have bigger problems. You are using MooTools and jQuery side by side which is ok, but if you don't know what you are doing, you can get into trouble quickly.

Upvotes: 2

shelman
shelman

Reputation: 2699

It's possible it's complaining because

document.getElementById("c50").getElementsByTagName("a")[0]

is not a function. Where is the fireEvent function coming from?

Upvotes: 1

Related Questions