ASP中ServerVariables集合用法详解(2)

作者:CODEOF 来源:21tx.com 时间:2007-09-14 10:26:00 

使用HTTP_USER_AGENT来检测浏览器的版本
  
    ServerVariables集合中,另外一个有用的值是用户浏览器的用户代理字符串。在“Detecting the Browser Type”页面(browsertype.asp),使用ServerVariables集合中的“HTTP_USER_AGENT”值来获得用户代理字符串,一些脚本用来解析该信息并寻找生产厂家名称和浏览器版本。


<% 
  strUA = Request.ServerVariables(“HTTP_USER_AGENT”) 
  Response.Write “The User Agent string is <B>” & strUA & “</B> 
  ” 
  If InStr(strUA, “MSIE”) Then 
  Response.Write “To upgrade your browser go to “_ 
  & “<A HREF=” & Chr(34) & http://www.microsoft.com/ie/”_ 
  & Chr(34) & “>http://www.microsoft.com/ie/<A> 
  ” 
  intVersion = Cint(Mid(strUA, InStr(strUA, “MSIE”) + 5, 1)) 
  If intVersion >=4 Then 
  Response.Write “You can use Microsoft Dynamic HTML” 
  End If 
  Else 
  If InStr(strUA, “Mozilla”) Then 
  If InStr(strUA, “compatible;”) = 0 Then 
  Response.Write “Your browser is probably Navigator. You can “_ 
  & “download the latest version of Navigator from “_ 
  & “<A HREF=” & Chr(34) & http://home.netscape.com/”_ 
  & “download/”& Chr(34) & “>http://home.netscape.com”_ 
  & “/download/</A> 
  ” 
  intVersion = Cint(Mid(strUA, InStr(strUA, “/”) +1, 1)) 
  If intVersion >= 4 Then 
  Response.Write “You can probably use Netscape Dynamic HTML” 
  End If 
  Else 
  strVersion = Mid(strUA, InStr(strUA, “compatible;”) + 12) 
  strProduct = Left(strVersion, InStr(strVersion, “ “)) 
  Response.Write “Your browser is Navigator-compatible. You can”_ 
  & “search for the manufacturer using a search engine, such as”_ 
  & “<A HREF=” & Chr(34) _ 
  & “http://www.altavista.digital.com/CGI-bin/query?q=”_ 
  & strProduct _ 
  & Chr(34) & “>http://www.altavista.com/</A> 
  ” 
  End If 
  End If 
  End If 
  %> 


对IE 5.0和Navigator 4.61的搜索结果分别不同,对于其他厂家的浏览器,可以得到一个链接在Alta Vista Web站点自动开始搜索厂家的名称。
  
    注意,Netscape在用户代理字符串中不提供厂家的名称,因而无法绝对保证一个浏览器一定是Navigator。 

使用HTTP_ACCEPT_LANGUAGE来检测浏览器的语言
  
    ServerVariables集合中另外一个有用的值是“HTTP_ACCEPT_LANGUAGE”,它包含了一个当浏览器安装时指定的,或硬编码进用户的地区版本的语言代码。语言代码的例子有en-us(英国、美国)、de-at(德国、澳大利亚)和es-pe(西班牙、秘鲁)。
  
    语言代码可以是一般的且省略方言标识:例如,在我们的站点Wrox者,大批浏览者都是将en(英语)作为语言代码。
  
    因此,可以检测语言代码并自动装载一个合适的特定地区或指定语言版本的页面。

StrLocale = Lcase(Left(Request.ServerVariables(“HTTP_ACCEPT_LANGUAGE”),2)) 
  Select Case strLocale 
   Case “en”: Response.Redirect “http://uk_site.co.uk/” 
   Case “de”: Response.Redirect “http://de_site.co.de/” 
   Case “fr”: Response.Redirect “http://fr_site.co.fr/” 
   ‘... etc 
   Case Else: Response.Redirect “http://us_sitel.com/” 
  End Select 


或者根据特定的方言,重定向页面:


strLocale = Lcase(Request.ServerVariables(“HTTP_ACCEPT_LANGUAGE”)) 
  Select Case strLocale 
   Case “en-gb”: Response.Redirect “http://uk_site.co.uk/” 
   Case “en-us”: Response.Redirect “http://us_site.com/” 
   Case “es-pe”: Response.Redirect “http://es_site2.co.pe/” 
   ‘... 
   Case Else: Response.Redirect “http://us_site1.com/” 
  End Select 


标签:ServerVariables,asp
0
投稿

猜你喜欢

  • 从p开始,循序渐进学习WEB标准

    2008-03-08 18:53:00
  • 树型结构在ASP中的简单解决

    2007-10-07 12:52:00
  • 关于CSS中字号控制的兼容性研究

    2010-01-23 12:48:00
  • 快速掌握ASP连接11种数据库的常用语法

    2008-11-28 15:32:00
  • MYSQL教程:表达式操作符和数据类型转换

    2009-02-27 15:51:00
  • 恢复master..xp_logattach(log explorer)

    2010-07-01 19:19:00
  • 淘宝首页代码调整

    2011-04-22 12:44:00
  • [翻译]JavaScript中对象的层次与继承

    2008-12-31 13:36:00
  • 一个较复杂的字符串截取函数

    2009-11-02 10:45:00
  • mysql使用LOAD语句批量录入数据

    2010-03-18 16:19:00
  • asp实现*号隐藏IP地址

    2008-08-10 18:51:00
  • 优化MySQL的数据库性能的八种方法

    2012-01-05 19:28:53
  • asp清理站点缓存代码

    2008-07-21 12:37:00
  • Js中的函数直接量

    2007-12-21 19:15:00
  • 简单代码屏蔽超级链接虚线框

    2008-02-03 11:34:00
  • asp中文件与文件夹常用处理函数(文件后缀、创建文件等)

    2011-02-20 11:00:00
  • 边框样式的写法总结

    2009-01-18 13:00:00
  • ASP实现类似Java中的Linked HashMap类

    2010-04-03 20:49:00
  • 认识MySQL数据库对服务器端光标的限制

    2009-03-25 17:35:00
  • class和id命名探讨

    2007-10-16 17:55:00
  • asp之家 网络编程 m.aspxhome.com