原创文章如转载,请注明:转载自 xajax学习站
本文链接 : http://xajax.dayanmei.com/14.htm

下面是一些可以用来检测用户浏览器是否支持cookie,javascript和ajax的HTML/JAVASCRIPT代码,你可以用这些代码将检测后是否支持的信息显示给用户
Helper functions for detecting cookie and Ajax support:
<script    type="text/javascript">
<!--
        
//    Cookie    handling    
        
var    Cookie    =
        {
            
read:    function    (name)
            {
                var    
arrCookies    =    document.cookie.split    (';    ');
                for    (var    
i=0;    i<arrCookies.length;    i++)
                {
                        var    
arrCookie    =    arrCookies[i].split    ('=');
                        
                        if    (
arrCookie[0]    ==    name)
                        {
                            return    
decodeURIComponent    (arrCookie[1]);
                        }
                }
                return    
false;
            },
        
            
write:    function    (name,    value,    expires,    path)
            {
                if    (
expires)
                {
                        var    
date    =    new    Date    ();
                        
date.setTime    (date.getTime    ()    +    (((((expires    *    24)    *    60)    *    60)    *    1000)));
                        
expires    =    ';    expires='    +    date.toGMTString    ();
                }
                else    
expires    =    '';
        
                if    (!
path)    path    =    '/';
        
                
document.cookie    =    name+'='+encodeURIComponent    (value)+expires+';    path='+path;
            },
        
            
remove:    function    (name)
            {
                
this.write    (name,    '',    -1);
            }
        }
        
        
//    Detects    if    can    set    a    cookie    in    the    browser
        
function    browserSupportsCookies()
        {
            
Cookie.write('cookiesEnabled',    1);
            var    
boolCookiesEnabled    =    Cookie.read('cookiesEnabled');
            
Cookie.remove('cookiesEnabled');
            if    (
boolCookiesEnabled    !=    1)
            {
                return    
false;
            }
            return    
true;
        }
        
        
//    Detects    if    the    browser    supports    Ajax    
        
function    browserSupportsAjax()
        {
            if    (
typeof    XMLHttpRequest    ==    "undefined"    &&    typeof    ActiveXObject    ==    "undefined"    &&    window.createRequest    ==    "undefined")
            {
                return    
false;
            }
            return    
true
        
}
        
        
//    Detects    if    the    browser    can    use    ActiveX    if    necessary
        
function    ActiveXEnabledOrUnnecessary    ()
        {
            if    (
typeof    ActiveXObject    !=    "undefined")
            {
                var    
xhr    =    null;
                try{
                        
xhr=new    ActiveXObject("Msxml2.XMLHTTP");
                }catch    (
e){
                        try{
                            
xhr=new    ActiveXObject("Microsoft.XMLHTTP");
                        }catch    (
e2){
                            try{
                                
xhr=new    ActiveXObject("Msxml2.XMLHTTP.4.0");
                            }catch    (
e3){
                                
xhr=null;
                            }
                        }
                }
                if    (
xhr    ==    null)
                {
                        return    
false
                
}
            }
            
            return    
true;
        }
-->
</script>


如果用户的浏览器不支持cookie,javascript和ajax的话显示错误信息给用户
<!--    Detect    support    for    cookies    and    Ajax    and    display    message    if    not    -->            
<
div    id="supportError">
        <
noscript>
            <
div>JavaScript    appears    to    be    either    disabled    or    unsupported    by    your    browser.
            
This    web    application    requires    JavaScript    to    function    properly.
            
Please    enable    JavaScript    in    your    browser    settings,
            or    
upgrade    to    a    browser    with    JavaScript    support    and    try    again.</div>
        </
noscript>
        
        <
script    type="text/javascript">
        <!--
        
        if    (!
browserSupportsCookies())
        {
            var    
msg    =    '<div>Cookies    appear    to    be    either    disabled    or    unsupported    by    your    browser.    '
            
msg    +=    'This    web    application    requires    Cookies    to    function    properly.    ';
            
msg    +=    'Please    enable    Cookies    in    your    browser    settings    ';
            
msg    +=    'or    upgrade    to    a    browser    with    Cookie    support    and    try    again.</div>'
            
            
document.write(msg);
        }
        
        if    (!
browserSupportsAjax())
        {
            var    
msg    =    '<div>Your    browser    does    not    appear    to    support    Ajax    technology.    '
            
msg    +=    'This    web    application    requires    Ajax    to    function    properly.    ';
            
msg    +=    'Please    upgrade    to    a    browser    with    Ajax    support    and    try    again.</div>';
            
            
document.write(msg);
        }
            
        if    (!
ActiveXEnabledOrUnnecessary())
        {
            var    
msg    =    '<div>ActiveX    appears    to    be    disabled    in    your    browser.    ';
            
msg    +=    'This    web    application    requires    Ajax    technology    to    function    properly.    ';
            
msg    +=    'In    Internet    Explorer    versions    earlier    than    7.0,    Ajax    is    implemented    using    ActiveX.    ';
            
msg    +=    'Please    enable    ActiveX    in    your    browser    security    settings    ';
            
msg    +=    'or    upgrade    to    a    browser    with    Ajax    support    and    try    again.</div>';
            
            
document.write(msg);
        }        
        -->
        
</script>
</div>

上一篇:从0.2升级到0.5的改变 下一篇:站长简介

时间:2007-10-27 | 分类:xajax 手册 评论:阅读:2053
标签:javascript,xajax

发表评论

好多网友都好踊跃,这是好的,但请不要发表无意义的评论,谢谢

最新文章 订阅xajax站最新文章

最新评论

热门文章