Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Browser Detect

Browser Detect

Scheduled Pinned Locked Moved Web Development
javascripthtmllinuxhelpannouncement
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rob Tomson
    wrote on last edited by
    #1

    I've never done javascript before but I need to be able to detect which browser the user is running. I've searched the net and copied some code to do this. It's simple and I understand what it does but I can't get the .html page to display different text per browser. This is what I have in my html: <!-- if(BrowserDetect()!="Internet Explorer") { document.write('Other'); } else { document.write('Internet Explorer'); } // --> and this is the code that I found on the internet: function BrowserDetect() { var detect = navigator.userAgent.toLowerCase(); var OS,browser,version,total,thestring; if (checkIt('konqueror')) { browser = "Konqueror"; OS = "Linux"; } else if (checkIt('safari')) browser = "Safari" else if (checkIt('omniweb')) browser = "OmniWeb" else if (checkIt('opera')) browser = "Opera" else if (checkIt('webtv')) browser = "WebTV"; else if (checkIt('icab')) browser = "iCab" else if (checkIt('msie')) browser = "Internet Explorer" else if (!checkIt('compatible')) { browser = "Compatible" version = detect.charAt(8); } else browser = "An unknown browser"; if (!version) version = detect.charAt(place + thestring.length); if (!OS) { if (checkIt('linux')) OS = "Linux"; else if (checkIt('x11')) OS = "Unix"; else if (checkIt('mac')) OS = "Mac" else if (checkIt('win')) OS = "Windows" else OS = "an unknown operating system"; } document.rs_browser=browser; document.rs_OS=OS; document.rs_version=version; return browser; } function checkIt(string) { place = detect.indexOf(string) + 1; thestring = string; return place; } When I view my page in either FireFox or IE it doesn't generate any errors but it also doesn't display any sort of text. Please Help, Rob Tomson -- There are 10 kinds of people. Those who understand binary and those who don't.

    N 1 Reply Last reply
    0
    • R Rob Tomson

      I've never done javascript before but I need to be able to detect which browser the user is running. I've searched the net and copied some code to do this. It's simple and I understand what it does but I can't get the .html page to display different text per browser. This is what I have in my html: <!-- if(BrowserDetect()!="Internet Explorer") { document.write('Other'); } else { document.write('Internet Explorer'); } // --> and this is the code that I found on the internet: function BrowserDetect() { var detect = navigator.userAgent.toLowerCase(); var OS,browser,version,total,thestring; if (checkIt('konqueror')) { browser = "Konqueror"; OS = "Linux"; } else if (checkIt('safari')) browser = "Safari" else if (checkIt('omniweb')) browser = "OmniWeb" else if (checkIt('opera')) browser = "Opera" else if (checkIt('webtv')) browser = "WebTV"; else if (checkIt('icab')) browser = "iCab" else if (checkIt('msie')) browser = "Internet Explorer" else if (!checkIt('compatible')) { browser = "Compatible" version = detect.charAt(8); } else browser = "An unknown browser"; if (!version) version = detect.charAt(place + thestring.length); if (!OS) { if (checkIt('linux')) OS = "Linux"; else if (checkIt('x11')) OS = "Unix"; else if (checkIt('mac')) OS = "Mac" else if (checkIt('win')) OS = "Windows" else OS = "an unknown operating system"; } document.rs_browser=browser; document.rs_OS=OS; document.rs_version=version; return browser; } function checkIt(string) { place = detect.indexOf(string) + 1; thestring = string; return place; } When I view my page in either FireFox or IE it doesn't generate any errors but it also doesn't display any sort of text. Please Help, Rob Tomson -- There are 10 kinds of people. Those who understand binary and those who don't.

      N Offline
      N Offline
      NeverHeardOfMe
      wrote on last edited by
      #2

      Try moving the first two lines of the BrowserDetect function - var detect = navigator.userAgent.toLowerCase(); var OS,browser,version,total,thestring; - (ie the variable declarations) outside of the function - they are global variables (at least 'detect' and 'thestring' are also used in the checkIt function, so...) cheers Phil

      R 1 Reply Last reply
      0
      • N NeverHeardOfMe

        Try moving the first two lines of the BrowserDetect function - var detect = navigator.userAgent.toLowerCase(); var OS,browser,version,total,thestring; - (ie the variable declarations) outside of the function - they are global variables (at least 'detect' and 'thestring' are also used in the checkIt function, so...) cheers Phil

        R Offline
        R Offline
        Rob Tomson
        wrote on last edited by
        #3

        That worked. Thanks! I guess I should keep an eye on what scope the variables are in. Thanks, Rob Tomson -- There are 10 kinds of people. Those who understand binary and those who don't.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups