How to share URL in Facebook Messenger through web ?

The following script enables you to share any link/URL in Facebook Messenger from webpage. We have used Facebook JavaScript SDK to achieve this.

The script opens a new window in desktop web browser that enables you to share any link to specified friend of yours. While in mobile browser, it opens up the Facebook Messenger app and the link can be shared easily with your friends.

Follow this video for complete guidance :

Full Source Code :

<div id="fb-root"></div>
<script async defer crossorigin="anonymous" src="https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v13.0&appId={YOUR_FACBOOK_APP_ID}&autoLogAppEvents=1" nonce="ywRETNx8"></script>



<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css">

<style type="text/css">
    body{
        background: #eef1f5;
    }
    .messenger-wrap{
        color: #0084ff;
        background: #fff;
        padding: 8px 20px;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%,-50%);
        text-decoration: none;
    }
    .messenger-wrap:hover{
        background-color: #0084ff;
        color: white;
    }
</style>

<a href="javascript:;" class="messenger-wrap" onclick="messengerShare()">
    <i class="fab fa-facebook-messenger"></i> 
    <span>Share on Messenger</span>
</a>

<script>
window.fbAsyncInit = function() {
    FB.init({
        appId: '{YOUR_FACEBOOK_APP_ID}',
        xfbml: true,
        version: 'v2.9'
    });
    FB.AppEvents.logPageView();
};


function isMobile() {
    const toMatch = [/Android/i, /webOS/i, /iPhone/i, /iPad/i, /iPod/i, /BlackBerry/i, /Windows Phone/i];
    return toMatch.some((toMatchItem) => {
        return navigator.userAgent.match(toMatchItem);
    });
}

function messengerShare() {
    url = 'https://reeteshghimire.com.np/';
    if (isMobile()) {
        window.location.href = "fb-messenger://share/?link=" + url;
    } else {
        FB.ui({
            method: 'send',
            link: url,
            redirect_uri: url
        });
    }
}
</script>

 



Recommended For You

About the Author: Admin