Thursday, August 25, 2011

Linking to Android Market applications on the web

While working on web version of Da Dictionary I've decided to add link to Android version. Web links to Android Market looks like this http://market.android.com/details?id=com.demosten.android.dadict where 'com.demosten.android.dadict' is the package name which is unique for Android Market. This seams to work well when opened from PC browser. However opening this link from Android is a different story. The best way to open it is directly in Android Market application which works with different URI scheme. It looks like market://details?id=com.demosten.android.dadict where again we see the same package name mentioned. While built-in Android browser plays it smart and open the link in directly in Market Application alternative browsers like Firefox does not do so and viewing web version of Android Market on an Android phone is far form what users expected to see.

My solution
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
  updateAndroidMarketLinks();
  // some more core here ... 
 
  function updateAndroidMarketLinks()
  {
    var ua = navigator.userAgent.toLowerCase();
    if (0 <= ua.indexOf("android")) {
      // we have android
      $("a[href^='http://market.android.com/']").each(function() {
        this.href = this.href.replace(/^http:\/\/market\.android\.com\//,
          "market://");
      });
    }
  }
});
</script>
</head>

<body>
<a href="http://market.android.com/details?id=com.demosten.android.dadict" target="_blank">Download for Android</a>
</body>

</html>
As you can see my solution is based on JavaScript and jQuery. The idea is to search for 'android' sub-string inside browser's UserAgent and if that is the case - replace HTTP URI part with Android Market URI part, inside all links.

I believe a JavaScript only version is not hard to do, but I prefer to use jQuery.

1 comment:

Google android apps development said...

Thanks for the programming script. I bookmarked it future references.
IPhone App Development| Android apps developer|