SiteExperts.com Logo Home | Community | Developer's Paradise | Jobs
User Groups | Site Tools | Site Information | Search

Inside Technique : Backend Browser Detection : Client vs. Server

There is no hard and fast rule to determine where you should do your detection. In many cases, client-side detection is used exclusively because your ISP does not allow user-defined server-side scripts to be executed. Below we discuss a few guidelines and tips:

Provide alternate UI at the server

If you visit InsideDHTML with a different browser, you will receive the same content organized in a user interface specifically designed for your client capabilities. The user interface is generated dynamically when you request the page.

Since the different interfaces use very different HTML, trying to do this negotiation on the client would be a very difficult if not impossible task. I would need to understand and code around various browser issues, the page would be much larger as it will contain code for all browsers, and maintaining the page would become much more difficult.

Use Standard HTML on the client

Don't write server scripts where there are already standard client-side solutions. For example, to provide a message to browsers that do not support scripting, use the NOSCRIPT element, rather than server-side code. Also, it is possible for your visitor's to disable scripting which can't be detected by the server.

Don't mix server and client scripts

Do not use ASP on the server to write into script blocks unless you are providing a completely alternative script block. Otherwise, your code quickly becomes unreadable and possibly unmaintainable. Also, your code is now dynamically generated making debugging a much more difficult taks. Below is a very simple example that hopefully demonstrates how this becomes confusing quickly:

<SCRIPT LANGUAGE="JavaScript">
  var browser = <% 
if (ie4) { 
Response.write("\"IE4\"") 
  } else { 
Response.write("\"Not IE4\") } %>
</SCRIPT>

Use Response.write() for one-liners

If you are doing simple conditional on the server that is outputting one or two lines based on the client, avoid writing code as follows:

<% if (ie4) { %>
  IE4 User
<% } else { %>
  Non-IE4 user
<% } >

You can optimize the above by writing the contents into the file using Response.write(), the server equivalent to the client's document.write() method. This is to minimize the fragmentation of your scripts in the document. Remember, server-side scripts require more processing power than just sending an HTML file to the client, and the more script blocks you define the slower the page may get. Therefore, you would rewrite the above script in a single block as follows:

< if (ie4) {
  Response.write("IE4 User")
 } else {
  Response.write("Non-IE4 User")
 } %>

Conclusion

Backend browser detection is the basis for our template model. In our next backend article, we will explain how we use a standard data structure to define our inside techniques user interface. The ad bar, navigation panels, related pages, and page list are all dynamically generated based on this data structure, while the article's content is almost always statically shared.

Discuss and Rate this Article