• 0

Hide Facebook divs if Facebook is blocked


Go to solution Solved by Seahorsepip,

Question

wrack

Hi,

 

I am working on a small site http://www.hagglehive.com and I want to use Facebook follow, like, share & comments plugin. http://www.hagglehive.com/en-au/Haggle/Detail/Lumia-930-Green-4G-Lte-32Gb-On-25-Aug-2014-Price-549-221 page has all of those plugin added.

 

I am using ASP.NET with .NET Framework 4.5. Below is the code at the bottom of the master page. 

<!-- Facebook Scripts -->
<div id="fb-root"></div>
<script>
	(function (d, s, id) {
		var js, fjs = d.getElementsByTagName(s)[0];
		if (d.getElementById(id)) return;
		js = d.createElement(s); js.id = id;
		js.src = "http://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.0";
		fjs.parentNode.insertBefore(js, fjs);
	}(document, 'script', 'facebook-jssdk'));
</script>

Below is one of the div where follow button should show up. 

<div runat="server" id="divFacebookFollowButton" class="fb-follow RoundBorder FacebookFollowButton" data-href="//www.facebook.com/HaggleHive" data-width="100%" data-colorscheme="light" data-layout="standard" data-show-faces="false"></div>

What I want to do is, hiding all Facebook related DIVs first and then using jQuery Ajax, I am trying to connect to Facebook and see if the result is a success. If so then I make them all visible. This is the part I am struggling with.

$(document).ready(function () {
    // Hide facebook divs first
    //$(".fb-follow").hide();
    //$(".fb-like").hide();
    //$(".fb-comments").hide();
    // Check if facebook is blocked
    setTimeout("ShowFacebookIfAvailable();;", 2500);
});

In the above call, I have commented out the part where it hides all divs first. So it is all working now and you can see Facebook div at the top but if I uncomment them then nothing shows up.

 

Below is the function "ShowFacebookIfAvailable".

function ShowFacebookIfAvailable() {
    window.jQuery.support.cors = true;

    $.ajax({
        type: 'HEAD',
        url: 'https://www.facebook.com',
        crossDomain: true,
        success: function (data, textStatus, jqXHR) {
            $(".fb-follow").show();
            $(".fb-like").show();
            $(".fb-comments").show();
        },
        error: function (jqXHR, textStatus, errorThrown) {
        }
    });
}

Any help would be good as I am stuck here.

 

BONUS : What I would ideally like is, first check if Facebook is blocked and only load the JS if not blocked, saving a little download and improving the performance a little. Not sure how to do this though!

 

Cheers :)

Link to post
Share on other sites

9 answers to this question

Recommended Posts

  • 0
Seahorsepip

kinda simpler method:

$(function() {
    var fb = new Image();
    fb.onload = function () {
        //facebook isn't blocked
    }
    fb.onerror = function () {
        //facebook is blocked
    }
    fb.src = "http://facebook.com/favicon.ico";
});

This code actually does not require jQuery but I used it anyway since I'm too lazy to write the js equivalent of $(document).ready

Link to post
Share on other sites
  • 0
wrack

This code actually does not require jQuery but I used it anyway since I'm too lazy to write the js equivalent of $(document).ready

 

Thanks :). That works a treat. Appreciate it.

 

One quick question. I see you used "var fb = new Image(); " to retrieve an image (icon). What type of variable to use if one were to check if a JavaScript can be accessed instead of an image?

Link to post
Share on other sites
  • 0
Seahorsepip

Thanks :). That works a treat. Appreciate it.

One quick question. I see you used "var fb = new Image(); " to retrieve an image (icon). What type of variable to use if one were to check if a JavaScript can be accessed instead of an image?

You can try the following code.

Though it might be a problem with domains that block requests from other servers like facebook for certain files and pages(as example you cant put facebook into a iframe on a website).

$(function() {
    var url = "http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js";
    var jsfile = new XMLHttpRequest();
    jsfile.onerror = function() {
        alert("blocked");
    };
    jsfile.onload = function() {
        alert("available");
    };
    jsfile.open("GET",url);
    jsfile.send();
});
 
Link to post
Share on other sites
  • 0
wrack

You can try the following code.

Though it might be a problem with domains that block requests from other servers like facebook for certain files and pages(as example you cant put facebook into a iframe on a website).

 

Yeah that makes sense to block certain things. I was asking this to do the same thing for the Google Ad as well as Google Analytics. Try to get the JS and if good then load the rest of the script.

 

So here is 1 more question (hopefully final and I hope you don't mind). Let's say it was successful in getting the file so I get into the onload function. How do I then use that object to inject into the webpage rather then reloading it again?

Link to post
Share on other sites
  • 0
wrack

Ahh never mind. Found it. I can do eval(). Looks like this can open my code up to xss attacks. Not going to do it for now then.

 

Thanks for all your help mate.

  • Like 1
Link to post
Share on other sites
  • 0
Seahorsepip

Ahh never mind. Found it. I can do eval(). Looks like this can open my code up to xss attacks. Not going to do it for now then.

 

Thanks for all your help mate.

Never load code from untrusted domains and always make sure to load them trough https to prevent js xss attacks on your users.

Link to post
Share on other sites
  • 0
wrack

That's true. I was going to load google analytics so hopefully it would be safe but you never know with crafty peoples.

Link to post
Share on other sites
  • 0
Seahorsepip

That's true. I was going to load google analytics so hopefully it would be safe but you never know with crafty peoples.

As long you get the analystics js from the official https source and not from a suspicious mirror it shouldn't be a problem. When you use https instead of http it's no longer possible to do any man in the middle attacks, given that the website and it's certificate can be trusted. But the website itself that is serving the js can still be hacked though in case of google analystics I'd more worried about the fact that google is hacked then the malicious js that is being served to your website.

 

So simply said, if you load official js libraries from facebook, google, yahoo, microsoft etc It should be fine as long you use a https url. Also malicious js should never be able to harm your website itself, it should only be able to harm your visitors by showing ads as example.

Link to post
Share on other sites
  • 0
wrack

Thanks. I will use https then.

Link to post
Share on other sites
This topic is now closed to further replies.
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By zikalify
      Ville de Bitche sees Facebook page restored after blocking
      by Paul Hill



      In yet another screw-up by Facebook’s overzealous algorithms, the French commune, Bitche, saw its page blocked from the site before being reinstated after the social media firm realised its error, according to a Politico report. It’s not the first time Facebook’s systems messed up this year, in January, Neowin reported that Facebook threatened to shutter a group after someone listed a collection point as Plymouth Hoe, a historical location in the British city, Plymouth.

      According to Valérie Degouy, who runs the page, an appeal was made to Facebook on March 19 explaining that Bitche was really the name of the area but received no response and in the meantime messaged the official French Facebook page but it responded saying it could do nothing and that Degouy had to wait for the appeal response.

      Despite the appeal being made nearly a month ago, Facebook only reinstated the page following the publication of Politico’s article which highlighted the issue. It raises the question of how many other people are affected by Facebook’s algorithms that wrongly interpret content on the site.

      Given the social network’s size, Facebook will likely never abandon its algorithms because they do legitimately shut down and prevent abusive material. However, it does show that Facebook still needs to work on the algorithms to detect obscure cases like this or improve its appeal process.

    • By indospot
      Users can now appeal to Facebook's Oversight Board to delete content
      by João Carrasqueira

      Facebook has announced that, starting today, users of either Facebook or Instagram can now appeal to the company's Oversight Board if reported content is left up on the platforms. In October of last year, Facebook had announced that the board was ready to start reviewing cases, but it was limited to appealing content that was removed, if the user disagreed with that decision. Now, if a user reports content and Facebook decides it doesn't go against its Community Standards, the user can request an additional review.

      When requesting further reviews by the Oversight Board, users can select a number of reasons for the appeal. For example, they may believe the content goes against the Community Standards, but they may also consider that the Community Standards aren't where they should be, which could potentially prompt Facebook to rework its policies.

      Facebook says it had to deal with a lot of questions when it considered opening up the Board for additional reviews, such as how to handle content that is reported by multiple people, potentially for different reasons, or when should the board stop listening to new appeals once it has accepted a case. To address those questions, the company has designed a process that allows multiple appeals to be filed under a single case, linked to the content that's being reported on. Every user who reports that content will have a chance to explain why they believe it should be removed, even if other users have already tried. On the other hand, if a case is accepted, users can continue submitting statements to the Oversight Board up until it begins deliberations.

      Another hurdle is privacy, and on that note, Facebook says that reporting users will need to give consent for their identifiable information to be used in a case's file. Finally, there's the matter of communicating with users, to which end the company will update the status of a specific appeal on the Oversight Board's website, whether it be to inform users that a case has been picked up by the board or to tell them the final deliberation on the case.

      This step represents an evolution of the Oversight Board's scope, and Facebook promises to continue increasing it over time. Presumably, this should help the social network be a platform that's more up to the users' standards.

    • By Usman Khan Lodhi
      Facebook will pin vaccine information to top of News Feed
      by Usman Khan Lodhi



      Facebook announced today that it would bring information regarding COVID-19 vaccines to more people, as states are now expanding the eligibility to all adults in the country. The state-specific information, which would be taken from local health departments, would detail how adults can get vaccinated.

      The information would be provided in the form of pinned messages at the top of the News Feed, and Facebook is reusing a similar method it used during the U.S. 2020 presidential election since both voting and vaccine rollout are handled by state and local authorities. Adults eligible to get the vaccine will be able to see these notifications across the U.S. and "nearly 20 countries." Facebook stated it would roll out the feature to other countries when they "expand vaccinations." The firm iterated that it has played a pivotal role in sharing vaccine information with its users.



      Last week, Sheryl Sandberg, Facebook's COO, stated that the firm would convert part of its headquarters in Menlo Park into a vaccination center.

    • By zikalify
      Facebook will use its HQ as a vaccination centre, says Sandberg
      by Paul Hill



      Sheryl Sandberg, Facebook’s Chief Operating Officer (COO), has announced that Facebook will convert part of its Menlo Park headquarters into a vaccination site for “hard-hit communities”. The conversion of part of its HQ will be done in participation with Ravenswood Family Health Network. Sandberg said the firm will also partner with the State of California and local non-profits to support mobile vaccination clinics in four of the state’s hardest-hit regions.

      In her post, Sandberg wrote:

      Sandberg shared a photo of a COVID-19 board giving directions to the vaccination hub at Menlo Park but she didn’t state whether vaccinations at the HQ have begun to be administered or whether they will start in the coming weeks or months.

      Facebook has been playing an active role in trying to get people to go out and be vaccinated. Earlier this month it began encouraging users to get vaccinated through profile picture frames and last month it released a tool that let people know where they could get a coronavirus vaccine.

    • By Steven P.
      Facebook is suffering a worldwide outage [Update]
      by Steven Parker



      It seems like Facebook is down worldwide as of posting. Upon checking downdetector.com to see if it was just me getting blocked again for posting memes, it seems evident that many people are reporting the outrage right now, with over 18,000 reports in the last 15 minutes:



      As you can see from the above image, the spike is rather large. Timestamps in the above image are shown in CET (Central European Time), which means it started happening at around 2:15PM PT / 5:15PM ET / 11:15PM CET.



      Right now you get the above when you try to load facebook.com, but as of 11:45PM CET / 2:45PM PT / 5:45PM ET it appears the facebook logo has returned. Although we know there will be some people working on getting services back up, it does seem to be a significant outage.

      People are also reporting that Instagram is down, but I can get to it just fine from The Netherlands, I still can't load Facebook. Others are also saying they can load Facebook too, so it appears to be regional, or maybe "just whatever the hell server node you're on!" Who knows?

      Update: Aaaand it's back as of 12:05AM CET / 3:05PM PT / 6:05PM ET. Downdetector.com shows reports peaked at just over 157,000 as of half an hour ago at 11:35PM CET / 2:35PM PT / 5:35PM.

      Update 2: According to a tweet from Jane Manchun Wong, even Facebook's internal site was down in an outage that also affected Instagram and Whatsapp. But in a later tweet, Wong confirmed that all of these services are now coming back online.