Click here to Skip to main content
15,499,289 members
Articles / Web Development / XHTML
Posted 1 Nov 2008

Tagged as


109 bookmarked

All You Want to Know About a Mobile Website

Rate me:
Please Sign up or sign in to vote.
4.92/5 (28 votes)
1 Nov 2008CPOL7 min read
The article explains all the aspects of a mobile website.
Image 1


Nowadays, many more people are going to develop their websites specially designed for mobile devices.

The reason for doing so is simple. The Website owner can reach a dedicated mass that are always having a specific search. Mobiles are handy, people are always connected and now we even don't forget to carry them as we don't forget our purse!

Mobile Web pages are slightly different than standard Web pages. Mobile Web pages will have smaller content and smaller images. An idle mobile Web page should be rendered on any mobile device in less than 2 seconds. So we should always limit the Webpage to a smaller size. 

Key Point

The Website should be displayed on mostly all GPRS enabled mobile phones, PDAs and all 1G, 2G and 3G mobile devices. This is a real challenge as you will not find a common solution for all the devices. I had Googled a lot for a common solution, but to my disappointment I could not find a common one for all. And this is the fact which led me to write this article.

What Changes are Required in Order to Convert a Normal Web Page into a Mobile Web Page?

There are two things which are important for a mobile browser. You should set both of them. 


    A DOCTYPE (Document Type Declaration) instructs the validator which version of the (X)HTML your Web page is using. You must write it to the very first line of your Web page. It helps to validate your markup and CSS.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. Content Type

    The content type of a mobile Web page can be any of the following:

    1. application/vnd.wap.xhtml+xml
    2. application/xhtml+xml

    Which one is correct from a or b depends on the mobile device. Some devices support a, some supports b and many of them supports both. The best way is to write a function which checks the Request Headers and then set the content type accordingly. I have presented that function below:

    /// <summary>
    /// Function to set the content type dynamically.
    /// You need to call this function on each mobile web page.
    /// </summary>
    public void setContentTypeDynamically(HttpRequest Request, HttpResponse Response)
    			("application/vnd.wap.xhtml+xml") != -1)
           Response.ContentType = "application/vnd.wap.xhtml+xml";
       else if (Request.Headers["Accept"].ToString().IndexOf
    				("application/xhtml+xml") != -1)
           Response.ContentType = "application/xhtml+xml";
           Response.ContentType = "text/html";

    The function is fairly easy to understand. It reads the value of server variable Request.Headers["Accept"] and sets the ContentType of response object accordingly. Here the order to check the ContentType is important, as many modern mobile browsers may support all of them!

Images are Special. Render them According to the Screen Size of a Mobile Device.

The general width of a mobile device was 255 pixels. But this has changed dramatically as mobile manufactures are producing N number of models with various screen sizes. You can check the screen size of a mobile device from X-Wap-Profile XML file. I have discussed the X-Wap-Profile in another point below.

The best way for image displaying is - Store a single image of 255 pixels in database and then resize it on ISS according to the requested screen width and height.

For example: Motorola L7 has 176 x 200 screen size (Width x Height), BlackBerry8800 has 320 x 240 screen size and Nokia2630 has 128 x 160 screen size. For the perfect view of the image, it must be resized on IIS accordingly and then you should point the correct URL to the image tag.

To ease the process, I have created a table in database. This reads the “X-Wap-Profile” on first request and stores model name of the mobile, URL of the X-Wap-Profile, screen width, screen height, supports 3gp, mp4, etc. information in the database table.

I don't go to read the 'X-Wap-Profile' each time a request comes for a Web page. Instead I do this only for the first time a new mobile is requested. On first request, all the device info is stored in the database table and for all the subsequent requests, I can look for height and width of a screen directly in my database. This will lessen the efforts of requesting the “X-Wap-Profile” each time and can serve a Web page faster to the client.

Note: The code to read the image from database and to resize the image is beyond the scope of this article. Please search The Code Project for this!

Single Domain, Two Different Web Sites

Sometimes, you may have only a single domain registered and you want two different Websites pointing towards the same domain name.

Let’s understand this with an example. You have a domain something like You will develop two different websites: Website ‘A’ fully dedicated for normal desktop browser and website ‘B’ with mobile Web pages which are smaller in size and specially designed for mobile browsers.

Now, if is requested from a desktop browser, you want to show the home page of your normal website ‘A’ and if the request comes from any GPRS enabled mobile device, you will want to show them the home page of your mobile page Website ‘B’.

Why You Want the Same Domain Name for Both of the Websites?

The answer is simple:

  1. You get the benefit of SEO on both of the pages.
  2. The same brand value and customer credit can be received on both of the sites.
  3. Marketing and Advertisement can be shared between both of the websites.
  4. You may also have the domain but this makes more sense to show a user mobile home page rather than a normal Web page, if it is requested from a mobile device. 

Code to Detect a Mobile Browser

Following is the code which solves the above issue. I have created a function which checks for the ‘X-Wap-Profile’. If it is found, then I redirect them to my mobile Web pages, else I redirect the request to normal Web pages.

// Declare a boolean variable for mobile browser
bool IsMobi = false;

// If request header can find the X-Wap-Profile, then it is a mobile browser
        if (Request.Headers["X-Wap-Profile"] != null)
            if (Request.Headers["X-Wap-Profile"].ToString().Length > 0)
                IsMobi = true;
                Response.Redirect("index.aspx", true);
                Response.Redirect("web/index.aspx", true);

What are Request.Headers?

HTTP request headers allow the client to provide information about itself to the server. They give additional details about the nature of the request like its content type, Accept-Charset, Accept-Encoding, Accept-Language, etc.

What is ‘X-Wap-Profile’?

When you request a mobile browser, it sends much more information back to the server. One of them is the link to the ‘X-Wap-Profile’.

  • X-Wap-Profile:

X-Wap-Profile is an XML file which provides all the detailed information about a mobile device. You can find the screen size of the mobile device, supported audio and Video types, Model name of the device and many more things.  

This XML file is provided by the phone manufacturer for the use of the outer world.

Will Only This Code Do For All GPRS Enabled Mobile Devices?

The answer is NO. But the trick is not much harder than above. Some mobile device don't have the attribute X-Wap-Profile. Instead, they may have any of the following:

  • X-Wap-Profile
  • X-Wap-Profile:
  • Profile
  • Profile:

Moreover, in my experience some Samsung and LG devices also have different writing style of the same attribute as shown below:

  • X-Wap-Profile
  • x-wap-profile

The best way is to check all of them in a single function and according to value of IsMobi boolean variable, redirect the user to a relevant Web page.

Some Tips and Tricks

  1. Put the mobile pages in parent folder and normal Web page in a child folder.

    This will avoid any redirection if a request comes from any mobile device. It can save your valuable CPU cycles. It will also help you to give a faster response to the user. No user can wait for more than two seconds for rendering of a page. An ideal mobile page should be rendered on any mobile device in less than 2 seconds.

  2. Iphone is a special device and I want my Website to render as a normal desktop Web page on Iphone. What should I do for this?

    In this case, you will check the name ‘iphone’ OR ‘ipod’ from the User Agent. If it is found, then you will need to set the Viewport attribute in a metatag. In the below example, I have to take a literal named ‘iphone’ in the Header section of the page like this.

    <head id="Head1" runat="server">
            <asp:literal id="iphone" runat="server"></asp:literal>

    And in the code behind, I check it as shown below:

    // If requesting User Agent is an IPhone or IPod than set the iphone label
    if (Request.UserAgent.ToLower().Contains("iphone"))
         // Set the Viewport attribute.
         iphone.Text = "<meta name=\"viewport\" content=\"width = 
    			device-width height = device-height \" />";
    else if (Request.UserAgent.ToLower().Contains("ipod"))
         // Set the Viewport attribute.
         iphone.Text = "<meta name=\"viewport\" content=\"width = 
    			device-width height = device-height \" />";


In my experience, I have tested the above code on more than 200 mobile devices and it has worked fine.

I have not included a demo project as the code shown above is very simple to understand and apply. Feel free to contact me if you have any questions in understanding the concept.


  • 1st November, 2008: Initial post


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

Written By
Software Developer (Senior)
India India
Mahesh has worked on and mobile technologies for last 4.6 years.

He has worked with mobile WAP site for more than a year.

The ideas presented here have been tested on more than 200 mobile devices. The logic presented here have got success on mostly all type of legacy to latest mobile devices including Sony, Nokia, Motorola, Samsung, Iphone and Gphone

Comments and Discussions

QuestionNice Article Pin
shahidsidd6-Jun-14 19:49
Membershahidsidd6-Jun-14 19:49 
QuestionJava Pin
rcjr2422-Nov-12 22:23
Memberrcjr2422-Nov-12 22:23 
GeneralMy vote of 5 Pin
SachinDakle16-Oct-12 6:06
MemberSachinDakle16-Oct-12 6:06 
GeneralMy vote of 5 Pin
adriancs15-Apr-12 19:16
professionaladriancs15-Apr-12 19:16 
QuestionConnect to sqlserver database Pin
sun_kkd5-Mar-12 7:24
Membersun_kkd5-Mar-12 7:24 
Generalrequest to show us the db table structure Pin
Rod DeValcourt23-Dec-09 10:39
MemberRod DeValcourt23-Dec-09 10:39 
Generalretrieving data from database on mobile websites Pin
ashish lade25-Oct-09 21:45
Memberashish lade25-Oct-09 21:45 
GeneralRe: retrieving data from database on mobile websites Pin
mahesh@indianic.com25-Oct-09 22:00
Membermahesh@indianic.com25-Oct-09 22:00 
GeneralRe: retrieving data from database on mobile websites Pin
ashish lade25-Oct-09 22:08
Memberashish lade25-Oct-09 22:08 
GeneralASP.NET : Mobile Device Detection [With Capabilities] And Redirection Pin
Amimpat23-Aug-09 21:46
MemberAmimpat23-Aug-09 21:46 
GeneralRe: ASP.NET : Mobile Device Detection [With Capabilities] And Redirection Pin
KillerTiger14-Nov-09 2:59
MemberKillerTiger14-Nov-09 2:59 
Generalgreat Pin
slamps22-Jul-09 3:41
Memberslamps22-Jul-09 3:41 
GeneralNice Pin
DavidRRR17-Jun-09 5:38
MemberDavidRRR17-Jun-09 5:38 
GeneralRe: Nice Pin
mahesh@indianic.com17-Jun-09 5:41
Membermahesh@indianic.com17-Jun-09 5:41 
Questionhow to detect mobile and it'sw webbrowser with code Pin
bhaiyalal7-Nov-08 23:30
Memberbhaiyalal7-Nov-08 23:30 
Generaldone great job Pin
HarshSaxena7-Nov-08 23:18
MemberHarshSaxena7-Nov-08 23:18 
rohan_puranik7-Nov-08 23:14
Memberrohan_puranik7-Nov-08 23:14 
Generalhi............ Pin
khansameer7-Nov-08 22:43
Memberkhansameer7-Nov-08 22:43 
Generalgreat admiration Pin
Member 26295094-Nov-08 21:07
MemberMember 26295094-Nov-08 21:07 
GeneralAwesome.. Pin
Vimal Panara4-Nov-08 21:03
MemberVimal Panara4-Nov-08 21:03 
GeneralGreat.. Pin
Rule Breaker4-Nov-08 13:15
MemberRule Breaker4-Nov-08 13:15 
GeneralExcellent Pin
victorbos3-Nov-08 15:50
Membervictorbos3-Nov-08 15:50 
GeneralExcellent.................... Pin
khansameer3-Nov-08 2:02
Memberkhansameer3-Nov-08 2:02 
Generalnice article Pin
Abu Mami1-Nov-08 21:14
MemberAbu Mami1-Nov-08 21:14 
GeneralRe: nice article Pin
mahesh@indianic.com2-Nov-08 18:13
Membermahesh@indianic.com2-Nov-08 18:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.