0

Due to business requirement reason, I need to generate the content using jQuery.get() method to populate the content of the HTML page. Now, the normal way to include social sharing creates a problem where the display of share window will not include the generated content done by jQuery.

The URL of the page is like http://server/productdetails.html?productid=12345...

This is the screenshot of sharing to Facebook ended like.

enter image description here

Is there anyway to work around this?

In case useful, here are the codes of the page:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<title>Insert title here</title>
<script src="https://code.jquery.com/jquery-latest.min.js"></script>
<script>
    function fbshareCurrentPage() {
        window
                .open(
                        "https://www.facebook.com/sharer/sharer.php?u="
                                + escape(window.location.href) + "&t="
                                + document.title, '',
                        'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=300,width=600');
        return false;
    }

    function display(title, description, sku) {
        $(document).prop('title', title);
        $('#description').html(description);
        $('#sku').html(sku);
    }

    function displayImage(imageUrl) {
        $("#image").attr("src", imageUrl);
    }

    function getURLParametersByName(paramName) {
        var sPageURL = window.location.search.substring(1);
        var sURLVariables = sPageURL.split('&');
        for (var i = 0; i < sURLVariables.length; i++) {
            var sParameterName = sURLVariables[i].split('=');
            if (sParameterName[0] == paramName) {
                return sParameterName[1];
            }
        }
    }

    function fetchProductDetails() {
        var param = getURLParametersByName("productid");
        var title, description, sku;
        $.ajax({
            type : "GET",
            url : "Products?action=loadProductByProductId&productId=" + param,
            contentType : "application/json; charset=utf-8",
            dataType : "json",
            async : true,
            cache : false,
            success : function(data) {
                //console.log(data);
                $.each(data, function(idx, obj) {
                    if (idx == "name") {
                        title = obj;
                    }
                    if (idx == "description") {
                        description = obj;
                    }
                    if (idx == "sku") {
                        sku = obj;
                    }
                });
                display(title, description, sku);
            }
        });
    }

    function fetchProductImage() {
        var param = getURLParametersByName("productid");
        $.ajax({
            type : "GET",
            url : "Products?action=loadProductMainImageByProductId&productId="
                    + param,
            contentType : "application/json; charset=utf-8",
            dataType : "json",
            async : true,
            cache : false,
            success : function(data) {
                //console.log(data);
                displayImage(data);
            }
        });
    }

    function resizeMe() {
        window.resizeTo(400, 240);
    }
</script>
</head>
<body onload="fetchProductDetails();fetchProductImage();">
    <table
        style="width: 480px; margin: auto; border-collapse: separate; border-spacing: 8px">
        <tr>
            <td colspan=2 style="text-align: center"><img
                src="images/blacktea2.png" style="width: 320px" id="image" /></td>
        </tr>
        <tr>
            <td colspan=2 style="width: 320px; text-align: justify"><label
                id="description">Lorem ipsum dolor sit amet, consectetur
                    adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris.
                    Maecenas congue ligula ac quam viverra nec consectetur ante
                    hendrerit. Donec et mollis dolor. Praesent et diam eget libero
                    egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut
                    porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula
                    ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur
                    adipiscing elit. Aenean ut gravida lorem. Ut turpis felis, pulvinar
                    a semper sed, adipiscing id dolor. Pellentesque auctor nisi id
                    magna consequat sagittis. Curabitur dapibus enim sit amet elit
                    pharetra tincidunt feugiat nisl imperdiet. Ut convallis libero in
                    urna ultrices accumsan. Donec sed odio eros. Donec viverra mi quis
                    quam pulvinar at malesuada arcu rhoncus. Cum sociis natoque
                    penatibus et magnis dis parturient montes, nascetur ridiculus mus.
                    In rutrum accumsan ultricies. Mauris vitae nisi at sem facilisis
                    semper ac in est.</label></td>
        </tr>
        <tr>
            <td style="text-align: left">SKU:<label id="sku">99999</label></td>
            <td></td>
        </tr>
        <tr>
            <td style="text-align: left">Register/Login To Buy</td>
            <td style="text-align: right"><a
                href="javascript:fbshareCurrentPage()" target="_blank">Facebook</a></td>
        </tr>
    </table>
</body>
</html>
d4v1dv00
  • 971
  • 4
  • 10
  • 21

1 Answers1

0

There are two possible solutions to your problem.

1) Send the info in the sharer.php url itself about the content of the page http://www.facebook.com/sharer.php?s=100&p[title]=YOUR_TITLE&p[summary]=YOUR_SUMMARY&p[url]=YOUR_URL&p[images][0]=YOUR_IMAGE_TO_SHARE_OBJECT (Refer: https://stackoverflow.com/a/6138879/1741052)

2) Use Meta tags on the page while you're filling the content via jquery also fill the meta tags on the page.

<meta property="og:title" content="The Rock"/> 
<meta property="og:url" content="http://www.imdb.com/title/tt0117500/"/>
<meta property="og:image" content="http://ia.media-imdb.com/rock.jpg"/>

(Refer: https://stackoverflow.com/a/12547228/1741052)

Community
  • 1
  • 1
Rohit Batra
  • 674
  • 4
  • 18
  • I tried both methods, the first method only managed to send the correct response link when visitor click on the post at my timeline. Still no picture, description and title display on the share window. 2nd method totally no luck. – d4v1dv00 Apr 28 '15 at 14:34
  • @d4v1dv00 try debugging any of your site's URL with the FB Debugger (https://developers.facebook.com/tools/debug/). And see what the result comes? – Rohit Batra Apr 29 '15 at 03:56
  • same old stuff as before. anyway, I had given up and change my page into server-side to generate the meta data. now the meta data shown. works fine on twitter but FB still not picking up image and description shown in share dialog box. besides settling the HTTPS certificate that FB is complaining, it asked of: type 'website' is invalid because a required property 'og:title' of type 'string' was not provided. any ideas? – d4v1dv00 Apr 29 '15 at 06:25
  • @d4v1dv00 can you give the link of your website? that might give me a better idea of the problem – Rohit Batra Apr 29 '15 at 06:39
  • i think i know why now. after i tried using https://developers.google.com/structured-data/testing-tool/ as introduced at http://ogp.me/, it seems like the cause my page is served in password protected environment using Shiro. so what i did was i whitelist the jsp page to try my luck. only then i realised i was using request.getRequestDispatcher("/productdetails.jsp").forward(request, response); and my page URL shown the servlet URL rather than the jsp URL. I can't open the servlet to public in order for FB to crawl. i know this is off-topic but one thing lead to another... – d4v1dv00 Apr 29 '15 at 08:13
  • i managed to sort out the servlet URL thingy but still my share dialog window refused to show the preview of picture and summary text. any ideas? – d4v1dv00 Apr 29 '15 at 13:50
  • @d4v1dv00 try that url in facebook debugger tool see if FB can scrape any info from that url? – Rohit Batra Apr 30 '15 at 03:32
  • just tried, the scrapped runs but it doesn't capture the summary text (Description parameter use is "desc"), not sure if i use the correct parameter? here is my URL of FB share: http://mywebsite.com/productdetails.jsp?productid=2&memberid=ID00000016&sku=12345&title=Gourmet+Black+Tea&imgUrl=http%3A%2F%2Fshop.mygoldenduck.com%2Fmedia%2Fcatalog%2Fproduct%2Fb%2Fl%2Fblacktea.png&desc=Premium+Black+Tea+imported+from+Japan%2C+full+of+nutritious+and+anti-oxidants – d4v1dv00 May 01 '15 at 03:56
  • @d4v1dv00 the parameter is this `og:description` Refer this for all Open Graph params : http://ogp.me/ – Rohit Batra May 04 '15 at 03:30