简体   繁体   中英

Using javascript to detect browser

I wanted to something like this on my web page: If the browser accessing the page is Internet Explorer 8.0 or earlier, I want to alert the user that you need to upgrade your browser and do not load the page. Else if you are using other browsers, then simply load the page and proceed normally.


The reason, I don't want to load the page in IE right now is because, the css3 goes crazy in IE, which work perfectly in Chrome, Mozilla etc. I am working on that and hope to get it fixed in a day or two. Meanwhile, I dont want anybody to be able to open my web page using IE 8.0 or earlier.


I tried these, but to no avail:

<script type="text/javascript">
    nav=navigator.appName;
    if (nav.indexOf("IE 8.0")!=-1)
    {
       alert("You are using an older version");
    }
    else
    {
       alert("Switch to a new version");
    }
</script>

I want to do this using Javascript. How can I do it ?

if($.browser.msie && parseInt($.browser.version, 10) <= 7) {
    //Do alert
}   

Using Modernizr on IE you got classes that detect the version of the browser in order to show or hide elements on the document using just CSS .

jQuery does have inbuilt browser detection :

if ($.browser.mise && $.browser.version <= 8) {
    alert("Get a better browser!");
}

A better solution is to use feature detection though.

jQuery made that easy.

if($.browser.msie){ ... }

https://github.com/jquery/jquery-browser

For non-jquery users I made a fork without jQuery dependence : https://github.com/yukulele/browser

if(browser.msie){ ... }

But use Modernizer for features detection is a better way : http://www.modernizr.com/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM