0

I'm working with jQuery Mobile.
My Code for move to details div ::

<a href="#details" data-ajax="true" data-transition="pop" data-role="button" data-inline="false" data-icon="info">Go To Details</a>

Now, I want to authenticate user with Facebook.

So I have applied ::

<a href="javascript:userLogin()" data-ajax="true" data-transition="pop" data-role="button" data-inline="false" data-icon="info"> Login with Faceook </a>

Function Calling for Authentication

function userLogin() {
     FB.login(
          function(response) {
               if (response.authResponse) {
                   alert('logged in');

                   FB.api('/me', function(response) {
                     alert("Welcome " + response.name);
                   });
                } else {
                   alert('not logged in');
                }
           },
           { scope: "email" }
     );
}

I'm getting valid user response after authentication.
Now I want to navigate to that details div.

So, how can I implement that ajax code after successfully login ??
Any suggestion will be appreciated.

Thanks.

2
  • You can set it as cookies and then redirect to detail page. Commented Apr 5, 2013 at 6:15
  • if you have jquery try $('a[href="#details"]').click() Commented Apr 5, 2013 at 6:17

2 Answers 2

1

Instead of changing the href attribute directly, you should bind the onclick event to a function returning true.

Modify your a tag.

<a href="#details" onclick="javascript:return userLogin()" data-ajax="true" data-transition="pop" data-role="button" data-inline="false" data-icon="info">Go To Details</a>

Modify your userLogin function:

function userLogin() {
    FB.login(
        function(response) {
            if (response.authResponse) {
                alert('logged in');

                FB.api('/me', function(response) {
                    alert("Welcome " + response.name);
                });
            } else {
                alert('not logged in');
            }
        },
        { scope: "email" }
    );
    return true;
}

It seems that FB.login is an asynchronized operation. If you want to redirect after the login action, try to modify the location.hash after login. Note that the return value has changed to false.

function userLogin() {
    var hash = '#details';
    FB.login(
        function(response) {
            if (response.authResponse) {
                alert('logged in');
                FB.api('/me', function(response) {
                    alert("Welcome " + response.name);
                    window.location.hash = hash;
                });
            } else {
                alert('not logged in');
            }
        },
        { scope: "email" }
    );
    return false;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Not working.. It directly go to that div without authenticate user.
Thanks mate. Solved..:) But I've edited in <a> tag and remove href attribute because it navigates before authenticated. Again Thanks..:)
0

Use $.ajax() for making a call to the Facebook API to authenticate and in the callback function of the ajax function, call the click event of the details link.

Maybe something like this,

  $.ajax({
     url : 'https://graph.facebook.com/me'+whatever,
     success : function(){
        $('#details').click()
     }
  });

The success function will be called when the ajax call is successful.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.