开发者

JavaScript error "Function is expected"

开发者 https://www.devze.com 2023-03-20 04:32 出处:网络
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

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.addListe开发者_JAVA技巧ner(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);
}


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.


It's possible it's complaining because

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消