Solved

Is there a way to automate clearing facebook's cache for a url?

Posted on 2016-10-04
2
63 Views
Last Modified: 2016-10-05
We have a page that changes daily but facebook's cache has the old stuff still displaying.  I can go and manually clear it using their tool but this is impractical ... is there a way to automate this process?
0
Comment
Question by:afacts
2 Comments
 
LVL 29

Accepted Solution

by:
Randy Downs earned 500 total points
ID: 41829851
Try this .

snippet ... can be included on every page of your site via a common Javascript file. Then, to clear a page’s Facebook cache, simply visit that page and add ?facebook_refresh to the end of the URL (or &facebook_refresh

//Check if facebook_refresh query string is present
if(window.location.search.indexOf("facebook_refresh") >= 0)
{
    //Feature check browsers for support
    if(document.addEventListener && window.XMLHttpRequest && document.querySelector)
    {
        //DOM is ready
        document.addEventListener("DOMContentLoaded", function() {
            var httpRequest = new XMLHttpRequest();
            httpRequest.open("POST", "https://graph.facebook.com", true);
            
            httpRequest.onreadystatechange = function () {
                if (httpRequest.readyState == 4) { console.log("httpRequest.responseText", httpRequest.responseText); }
            };
            
            //Default URL to send to Facebook
            var url = window.location;
            
            //og:url element
            var og_url = document.querySelector("meta[property='og:url']");
            
            //Check if og:url element is present on page
            if(og_url != null)
            {
                //Get the content attribute value of og:url
                var og_url_value = og_url.getAttribute("content");
                
                //If og:url content attribute isn't empty
                if(og_url_value != "")
                {
                    url = og_url_value;
                } else {
                    console.warn('<meta property="og:url" content=""> is empty. Falling back to window.location');
                }               
            } else {
                console.warn('<meta property="og:url" content=""> is missing. Falling back to window.location');
            }
            
            //Send AJAX
            httpRequest.send("scrape=true&id=" + encodeURIComponent(url));
            
        });
    } else {
        console.warn("Your browser doesn't support one of the following: document.addEventListener && window.XMLHttpRequest && document.querySelector");
    }
}

Open in new window

0
 

Author Closing Comment

by:afacts
ID: 41830596
Thank you!!!!!!! This worked!!!!!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
Owning a franchise can be the dream of a lifetime. It provides a chance for economic growth. You can be as successful as you want.  To make your franchise successful, you need to market it successfully. Here are six of the best marketing strategies …
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now