The presence of #xfbml=1 is leading to some issues

Within my website, I am utilizing two distinct mechanisms incorporating both xfbml and fbjs:

  • An FB:Like tag for individual entries
  • The FB object for Facebook logins with FB connect

The issue arises when "all.js" is included on the page, as the login script functions properly but the fb:like tag does not.

However, by including "all.js#xfbml=1", the fb:like tags begin working again. Nevertheless, this causes my FB object to become undefined and disrupts the functionality of my login code.

Is there a component or step that I might be overlooking in this scenario?

Any assistance you can offer is greatly appreciated. Thank you!

Answer №1

To simplify your code, make sure to include the script reference only once and call init on the script when the page loads. Whether you are using the fb:like buttons or the login script, this method will work for both.

<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
  FB.init({
    appId  : 'YOUR APP ID',
    status : true, // check login status
    cookie : true, // enable cookies to allow server access
    xfbml  : true  // parse XFBML
  });
</script>

The crucial step is to set xfbml to true (this is equivalent to #xfbml=1). Without this setting, the like buttons will not render correctly.

If you wish to subscribe to the login event, simply add the following script after the FB.init() call:

  FB.Event.subscribe('auth.login', function(response) {
    window.location.reload(); // or any other action...
  });

Below is the login script that will be triggered ONLY when a user clicks on a link, button, etc.

function doLogin() {
   FB.login(function(response) {
     if (response.session) {
       if (response.perms) {
         // User is logged in and has granted permissions.
         // Perms is a list of granted permissions
       } else {
         // User is logged in but did not grant any permissions
       }
     } else {
       // User is not logged in
     }
   }, {perms:'read_stream,publish_stream,offline_access'});
}

Answer №2

Make sure to insert this xmlns attribute into your html element:

xmlns:fb="http://www.facebook.com/2008/fbml"

Don't forget to include the "all.js" file as well!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Conflicting issues with jQuery's load() method

I am facing an issue with loading two HTML pages into a single div. I have index.html and try.html in the root folder, where try.html is loaded into index.html's div (#content) when Button 01 is clicked on index.html. The problem arises when I further ...

employ the inverse Euler application

var a = new THREE.Euler( 0, 1, 1.57, 'YXZ' ); var b = new THREE.Vector3( 1, 0, 1 ); var c = b.applyEuler(a); In order to get the value of c using b.applyEuler(a), I am looking for the reverse operation involving applyEuler. Given that the value ...

Explore a JSON array within another JSON array

Here is a JSON format that I have: { "Project [id=1, dateDebut=2017-01-13, dateFin=2017-01-18, description=qsd, sponsor=qsd ]" : [ {"id":1,"title":"qsd ","description":"qsdqsd","dateFin":"2017-01-26"}, {"id":2,"title":"sss ","descriptio ...

Slowly revealing sticky navigation with slideDown animation using JQuery

Here is the code for a .JS file: $(document).scroll(function (){ var menuheight= $('header').height(); var y = $(this).scrollTop(); if (y>(menuheight)) { $('.menu_nav_features').addClass ...

Node.js is throwing an error stating that it cannot read the property 'id' of a null value

I have been working on upgrading my old Discord bot from Node.js version 6.x.x to 8.x.x and organizing the commands in a separate folder for better organization. The command was functioning properly with my old bot, but it seems to be encountering issues w ...

Color in the diamond shape only to a designated height

I am in search of a unique diamond shape that allows me to fill the color up to a specific height based on an attribute or variable provided. https://i.stack.imgur.com/62U6h.png. I attempted creating the diamond shape and initially considered placing it ...

The .NET controller does not receive traffic for the GET method

Having some trouble populating a table with JSON data using angular ng-repeat. No errors are showing up in the console, and my breakpoint in the .NET Controller isn't being triggered. Here's the code for the App Controller: var app = angular.mo ...

What is the best way to capture data sent by Express within a functional component?

const Header = (props) => { const [ serverData, setServerData ] = useState({}); useEffect(() => { fetch('http://localhost:4001/api') .then(res => res.json()) .then((data) => { setServerData(data); ...

Tips for sending repeated ajax requests for multiple forms within a step-by-step wizard

I am currently using a plugin to develop a wizard with steps. I have encountered an issue where the ajax call is being triggered on every step transition, leading to infinite calls. To address this problem, I implemented a check to prevent multiple ajax ca ...

Determine whether a specific text is contained within a given string

Can someone help me determine if a specific text is included in a string? For example, I have a string str = "car, bicycle, bus" and another string str2 = "car" I need to verify if str2 exists within str. I'm new to JavaScript so I appreciate you ...

What are the steps to applying strike-through text in Vue.js?

I am currently working on a to-do application using Vue.js and I want to implement a feature where the text rendered in HTML has a line through it when the user checks an item off the list. Here is the snippet of my code: <html> <head> & ...

The automatic form completion feature in JavaScript failed to function

The last 2 rows starting from "request.done...." are not functioning correctly. Nothing happens when these lines of code run, even though everything else works perfectly Here is the script I am using: $(document).ready(function () { $('#retriev ...

Adjust the code to enhance the functionality of web components in Sharepoint

I recently came across some code online that I'm trying to modify in order to add an expanding button next to a web part on my Sharepoint site. However, the problem is that by default, all web parts are already expanded and require a click to collapse ...

How can we consolidate an array of objects containing 6 keys into fewer keys?

Apologies for any confusion caused by the title. As a newcomer to JavaScript, I may not be able to articulate exactly what I am trying to achieve. Hence, I will showcase my code and explain the desired outcome instead. Here is an array of objects I am wor ...

angularjs Populate input fields with default values within ng-repeat loop

Our challenge is to display input text with pre-filled values within a list using the ng-repeat directive. <ul ng-repeat="post in postList> <input type="text" ng-model="postid" nginit="postid='{{post.id}}'"></input> </u ...

Can someone provide guidance on creating a JavaScript function that locates an image within an <li> element and sets that image as the background-image property in the li's CSS?

Let's dive deeper into this concept: <li class="whatever"> <img src="/images/clients/something.jpg"> </li> <li class="whatever"> <img src="/images/clients/whatever.png"> </li> He ...

Using both CASE and MATCH operators within an array in Neo4j's Cypher Query Language (

Using the code snippet below, I am attempting to retrieve all details related to user data where the checked value is either 1 or 0. I have noticed that 'WHERE flight.checked IN check' does not seem to be properly working. Is it appropriate to u ...

Is It Possible to Use Separate Stylesheets for Different Web Browsers?

I have been trying to implement a JavaScript code that loads a specific stylesheet based on the user's browser. However, it seems like the code is not functioning correctly as none of the stylesheets are being displayed. I have meticulously reviewed t ...

Count the number of URL segments using JavaScript and jQuery

Can anyone suggest an efficient method to count the number of segments in a URL using JavaScript/jQuery? For instance: www.myexampleurl.com/section1/section2/section3 should output 3 ...

Using Javascript to search and refine a JSON field based on a specific string

I am attempting to use JavaScript to filter a JSON field based on a string input. Essentially, I have a search box and a simulated JSON response. When I type letters into the search box, an ajax call should filter my simulated response based on the input s ...