Thursday, December 8, 2011

Redirect to a mobile site

It's quite useful piece of code-behind, which redirects to a mobile version of the site.

Please use and modify for your needs. This routine is being written in VB.NET

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim strUserAgent As String
Dim flag As String
flag = "0"
strUserAgent = Request.UserAgent.ToString().ToLower()
If (strUserAgent.Contains("iphone")) Then flag = "1"
If (strUserAgent.Contains("blackberry")) Then flag = "1"
If (strUserAgent.Contains("mobile")) Then flag = "1"
If (strUserAgent.Contains("windows ce")) Then flag = "1"
If (strUserAgent.Contains("opera mini")) Then flag = "1"
If (strUserAgent.Contains("palm")) Then flag = "1"
If (strUserAgent.Contains("android")) Then flag = "1"

If (strUserAgent.Length <> 0) Then
If (Request.Browser.IsMobileDevice = "True" And flag = "1") Then
Response.Redirect("http://www.test1.org")
End If
End If
End Sub

No comments:

Post a Comment