Ryan Ryan - 1 year ago 61
iOS Question

Create a fallback URL if app is not installed using javascript

I'm attempting to make a plugin that converts normal links into deep links when a user is browsing on an Android or iOS device. So, for example, it finds the link and replaces it with the deep link like this:

var facebookURL = $('[href*="facebook.com"]');
var facebookDeep = 'fb://profile/123456789';
$(facebookURL).attr('href', facebookDeep);

When the link is clicked and Facebook is installed, the link will open up the Facebook app. If Facebook isn't installed, however, when the link is clicked, nothing happens. So I figured that I would just automatically open the web URL if there was still a focus on the frame after 1000ms:

if ($(this).is(":focus")) {
location.href = 'https://facebook.com';
} else {
console.log('Focus was lost');
}, 1000);

But this logs Focus was lost.

Any ideas on another way I can accomplish this? Or if there's an easier way to create a fallback.

Answer Source

Change this:

if ($(this).is(":focus")) {

to this:

if ($(facebookURL).is(":focus")) {

More info: The 'this' problem

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download