-3

I found that Safari does not support click() of Jquery.

So I have the following two implementations for click event:

For Safari:

var foo = document.getElementById(some_id.id);
var ce= document.createEvent("MouseEvent");
ce.initEvent("click", true, true);
foo.dispatchEvent(ce);

For other browsers other than Safari:

document.getElementById(some_id.id).click();

Now, my problem is, if I put both pieces into my code, I have to apply some sort of conditional to check, if one fails, use the other. Yet I'm unable to find a way to check that.

When I use document.getElementById(some_id.id).click(); Safari gives the following error:

TypeError: 'undefined' is not a function (evaluating 'document.getElementById(del_id.id).click()')

How could I catch&handle this error, or check if this error occurs in a conditional? So I could make the code to try the other piece?

I tried typeof (document.getElementById(some_id.id).click()) == undefined but no luck.

Thanks,

eastboundr
  • 1,857
  • 8
  • 28
  • 46
  • 1
    `x.click()` *calls* the function. If you wish to simply see of `x.click` is not defined: `x.click === undefined` - but doesn't jQuery handle the details with `$(elm).trigger("click")`? – user2864740 Mar 10 '14 at 23:14
  • my guess: if(jQuery.prototype.click){//click-function is defined} – HCS-Support Mar 10 '14 at 23:15
  • jQuery supports the `$(Element).click()` for all Browsers. – StackSlave Mar 10 '14 at 23:17
  • two more points for rep and I too can down vote this , as its not even jQuery for the selector of the element. – alexmac Mar 10 '14 at 23:20
  • No I do not think jQuery supports click() in safari, please see http://stackoverflow.com/questions/12744202/undefined-is-not-a-function-evaluating-el-click-in-safari – eastboundr Mar 10 '14 at 23:34

1 Answers1

1

Remove the parenthesis. In your comparison use only: .click === undefined.

celerno
  • 1,367
  • 11
  • 30