开发者

How do i turn this jQuery to javascript?

I have a facebook connect button on this site here is the code

 <fb:login-button onlogin="javascript:jfbc.login.login_button_click();"
 perms="email,publish_stream,user_about_me,user_hometown,user_location,开发者_StackOverflowuser_birthday,user_religion_politics,user_interests,user_activities,user_website" 
size="medium" v="2"><a class="fb_button fb_button_medium">
<span class="fb_button_text"\>Login With Facebook</span></a></fb:login-button>

and i want to trigger this button with a javascript call and doing research i found this jquery that seems that it would do the trick (havent tested though) and i was wondering if there is an equivelent javascript or mootool because jquery is not installed. I can install it if i cant find a solution. Or if anyone has another idea on how to trigger this facebook button

$("fb\:login-button").trigger("click");


There are two ways to "trigger" a listener:

  1. call it directly (e.g. element.onclick())
  2. dispatch an event into the DOM that the listener will respond to

The trouble with the first method is that it doesn't replicate a bubbling event so the listener may not work as intended (e.g. there is no associated event object or bubbling, the listener's this keyword may not be correctly set).

The trouble with the second is that some browsers will not allow programatically dispatched events to do certain things (click on links for example). Also, in some browsers you have to use the W3C dispatchEvent and in others the Microsoft fireEvent.

So unless the listener has been designed specifically to work with one method or the other and is called appropriately, your chances of triggering the listener successfully are quite low.

PS. Some libraries provide their own event system, with custom events and bubbling of otherwise non-bubbling events, but in that case you have to set and trigger the listener using that library, otherwise it will probably not respond to either of the above methods.


You should be able to just invoke the same code that is invoked inline:

jfbc.login.login_button_click();


I suppose it would be something like

document.getElementsByTagName("fb\:login-button")[0].click();

I'm sure that would work very well with a "normal" DOM element that handles the click event; however, I'm not entirely sure it will work in all browsers with the fb:login-button element shimmed into HTML. You'll have to let me know.


Looks like you should be able to do:

document.body.getElementsByTagName("fb\:login-button")[0].click();


It looks like you want a namespaced element selector, so you should use:

document.getElementsByTagNameNS('fb', 'login-button')[0].click();

The : is the namespace separator.


I ran into this tonight, absolutely positioned a new button image over the iframe, and was planning on using pointer-events:none to pass through and click the iframe, but I was looking for a cross-browser solution, here you go.

jQuery('.button_fb_connect').live('click', function(){  FB.login() })

Your simply running the js function FB.login() after clicking your new element, obviously you can use whatever event you want.

Thats in jQuery of course, but thats the function you want, not just a simple click event trigger.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜