English 中文(简体)
Facebook Open Graph - Like Button - Hidden Content Revealed Upon Like
原标题:

I was wondering, I wish to have a landing page with Facebook s Open Graph (specifically the like button) and I wanted to basically have content set to display:none (can t remember the specific until a user likes a page. An example being a div on an e-commerce store that when a user likes the page, the div is set to display:block and users can redeem a coupon code for discount.

I would like to be able to do this with div displays.

I saw this little snippet of code on the Facebook developers forum:

| ( taken from http://fbrell.com/xfbml/fb:like ) (Note: The event that s fired when you click "like")

<script>
  // this will fire when any of the like widgets are "liked" by the user
  FB.Event.subscribe( edge.create , function(href, widget) {
    Log.info( You liked   + href, widget);
  });
</script>

Can this be modified to effective set a div from display:none to display:block?

Thank you SO.

问题回答

If you specifically want to update your div to display:block, use...

<script>
  // this will fire when any of the like widgets are "liked" by the user
  FB.Event.subscribe( edge.create , function(href, widget) {
    $( #divid ).css( display , block );
  });
</script>

One caveat...

Your edge.create event WON T fire unless the URL you use as part of your call is EXACTLY the same as the URL you re attempting to fire this from.

I had a site I was running on example.DEV (my local laptop dev env) which referenced a link on example.com (the live server) and anything in the FB.Event.subscribe() block wouldn t run until I uploaded the file, and fired it from the LIVE server.

That could be why you re having trouble if it s not working so far!

I found the following solution, but you will need jQuery to do it. (possibly not, but that s what I used to do it).

Here is the code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="facebook.js"></script>
<script src="jquery.min.js" type="text/javascript"></script>

</head>

<body>
<div id="fb-root"></div>
<script type="text/javascript">
<!--
window.fbAsyncInit = function() {
 FB.init({appId:  133387220039676 , status: true, cookie: true, xfbml: true});
 FB.Event.subscribe( edge.create , function(href, widget) {
 // Do something, e.g. track the click on the "Like" button here
    $( #slidingDiv ).show();
 });
};
(function() {
 var e = document.createElement( script );
 e.type =  text/javascript ;
 e.src = document.location.protocol +  //connect.facebook.net/en_US/all.js ;
 e.async = true;
 document.getElementById( fb-root ).appendChild(e);
 }());
//-->
</script>
<fb:like href="http://www.facebook.com/TheRightMind" layout="standard" show-faces="false" width="450" action="like" colorscheme="light"></fb:like>

<div id="slidingDiv" style="display:none;">
Fill this space with really interesting content that you can
</div>

</body>
</html>

Just replace the Log.info() call with JavaScript that displays the div. In jQuery:

<script>
  // this will fire when any of the like widgets are "liked" by the user
  FB.Event.subscribe( edge.create , function(href, widget) {
    $( #yourdiv ).show();
  });
</script>

After verifying that liking the page will trigger that code (try console.log("AOK!")), you can make a <div> appear using this code:

Plain Old JavaScript:

FB.Event.subscribe( edge.create , function(href, widget) {
  document.getElementById("topsecret").style.display = "block";
});

The HTML:

<div id="topsecret" style="display:none;">
  Content Goes Here
</div>

If you re using the jQuery library or something similar, you can use a show() function to make the box appear instead:

FB.Event.subscribe( edge.create , function(href, widget) {
  $("#topsecret").show();
  //$("#topsecret").slideDown("fast");
  //$("#topsecret").fadeIn("fast");
});

You might also consider using an AJAX load command instead, because otherwise it s fairly easy to make the box appear when the content is only hidden.

FB.Event.subscribe( edge.create , function(href, widget) {
  $("#topsecret").load("mycontent.html");
});




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签