Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am developing a WordPress theme where a hyperlink (Facebook page) should take the user to Facebook app if installed or open a new tab if not installed.

here is the anchor/hyperlink:
HTML
<a id="facebook" class="social-btn facebook" title="Our Facebook Page" target="_blank" href="<?php echo esc_url( __( 'https://www.facebook.com/rainbowparentinguae' ) ); ?>">
    
</a>


and this is the JavaScript:
JavaScript
class FaceBook {

constructor() {
    this.FacebookBtn = document.getElementById("facebook");
    this.events();
}

events() {

    if (this.FacebookBtn) {
        this.FacebookBtn.addEventListener("click", this.openFacebook.bind(this));       
    }
}

openFacebook(e) {
    const IS_IOS = (navigator.userAgent.match(/iPad/i) != null) || (navigator.userAgent.match(/iPhone/i) != null) || (navigator.userAgent.match(/iPod/i) != null);
    const IS_ANDROID = (navigator.userAgent.match(/android/i) != null) || (navigator.userAgent.match(/Android/i) != null);


    if(IS_IOS) {
        setTimeout( function () { getURL(); }, 25);
        this.FacebookBtn.href = "fb://profile/1692101410811137";
    }

    else if (IS_ANDROID) {
        setTimeout( function () { getURL(); }, 25);
        this.FacebookBtn.href = "fb://page/1692101410811137";
    }

e.stopPropagation();
};

getURL() {
    location.href("https://www.facebook.com/rainbowparentinguae", '_blank' );
};
}

export default FaceBook;



I expect that if the user is using a computer (desktop/laptop) the Facebook page will open in new (windows/tab).

if the user is using iOS the page will open in Facebook App (if it is installed), in case the App is not there the Facebook page will be opened in a new (windows/tab).

if the user is using Android the page will open in Facebook App (if it is installed), in case the App is not there the Facebook page will be opened in a new (windows/tab).

What I have tried:

I have done some searches and developed a short code where everything is quite done except the scenario if Facebook App is not installed.
Posted
Updated 16-Sep-19 2:51am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900