简体   繁体   中英

java script and open graph facebook

I'm running a javascript application and a part of it runs true like this:

document.write("<param name='nick' value='" + nick + "'>\n");

In the <param ....> I cant call the graph api url (this give me a javascript error) so I need to call it before, then take it like a variable and put it in the <param> . Now I tried a lot of ways to do this without any luck, so I need some help for doing this:

call "the_url/me?fields="firstname" <- this works every time, php, java, html,... 

and now what seems to give me a lot of problems, set the response in a variable or something like this:

nick = arg1[]
id = arg2[]
and take it in the document.write() 

document.write("<param name='nick' value='" + nick + "'>\n");
document.write("<param name='id' value='" + id + "'>\n");

Sometimes it sets everything in the first variable; other times it returns nothing or the wrong thing in the wrong place or .... (you get the point i think).

Maybe I'm missing something, overlooking a simple way or not using the return right, I'm new to the opengraph thing and maybe I dont have it right .... I don't know anymore so I need help or advice

Update:

i did use fb.api like this

    function ShowMyName() {
        FB.api("/me", function (response) {
            document.jform.nick.value = response.name ; 
                });
}

then use the form so people have the option to change there nick before they start, the wierd thing about it is when i call the functoin with a alert it gives me the name but the other part give's me a undifent return, thats why i asked for help, i don't get why the msgbox works on the same script and the other part stays undifent, its the same script

so this works:

    <button name="my_full_name" onclick="testbut()" value="My Name" />
<script language="javascript" type="text/javascript">
function testbut() {
        FB.api("/me",
                function (response) {
                    alert('Name is ' + response.name);
                });

    }
</script>

and on the same script this won't work,

    function ShowMyName() {
        FB.api("/me", function (response) {
            document.jform.nick.value = response.name ; 
                });
}

i'm sure the document is right because i can put anythin in the value part and that wil show up ....

I hate to give an answer that starts with a documentation link, but it looks like this is your first application. This exact question is answered by the documentation as an example here: https://developers.facebook.com/docs/reference/javascript/

This call must be made asynchronously as you are waiting for remote data. You then need to execute a callback function upon receipt of data, otherwise your variables will be empty and your code will break. The OpenGraph JavaScript API works well to do this for you.

Basically, you need to make the call asynchronously and exec the document.write() calls as part of your callback function. So, after you've authenticated and all that, do something like this:

FB.api('/me', function(response) {
  nick = response.name;
  document.write("<param name='nick' value='" + nick + "'>\n");
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM