Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8661369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:28:53+00:00 2026-06-12T16:28:53+00:00

I’m writing a Device & Feature detection library (not really for any real use,

  • 0

I’m writing a Device & Feature detection library (not really for any real use, just experimenting and playing around), and as far as the device detection, I should be done, but I have just one (I think) issue. My code:

var $device = {

    mobile: {
        android: navigator.userAgent.match(/Android/i),
        blackberry: navigator.userAgent.match(/BlackBerry/i),
        iphone: navigator.userAgent.match(/iPhone/i),
        ipod: navigator.userAgent.match(/iPod/i),
        opera: navigator.userAgent.match(/Opera Mini/i),
        windows: navigator.userAgent.match(/(Windows Phone OS|Windows CE|Windows Mobile|IEMobile)/i)
    },

    tablet: {
        ipad: navigator.userAgent.match(/iPad/i),
        galaxy_tab: navigator.userAgent.match(/(GT-P1000|GT-P1000R|GT-P1000M|SGH-T849|SHW-M180S)/i),
        windows: navigator.userAgent.match(/Tablet PC/i)
    },

    desktop: {
        other: navigator.userAgent.match(/(Linux|X11)/i),
        mac: navigator.userAgent.match(/(Macintosh|Mac OS X)/i),
        windows: navigator.userAgent.match(/Windows/i)
    }

}

$device = {
    mobile: {
        any: $device.mobile.android || $device.mobile.blackberry || $device.mobile.iphone || $device.mobile.ipod || $device.mobile.opera || $device.mobile.windows
    },

    tablet: {
        any: $device.tablet.ipad || $device.tablet.galaxy_tab || $device.tablet.windows
    },

    desktop: {
        any: $device.desktop.other || $device.desktop.mac || $device.desktop.windows
    }
}

if($device.mobile.any) {
    $device = {
        type: "Mobile Phone"
    }
} else if($device.tablet.any) {
    $device = {
        type: "Tablet"
    }
} else if($device.desktop.any) {
    $device = {
        type: "Desktop"
    }
}

if($device.desktop.windows){alert("Hello")}

Now, as far as the userAgent strings go, I’m not fully convinced that they are all correct and I have not tested them yet, however this is not my problem. My problem is, that it won’t alert “hello”, and the error message in google chrome’s developer tools is: Uncaught TypeError: Cannot read property “windows” of undefined

Now, with my experience with Google’s tool, this is telling me that somewhere my syntax is incorrect, and not my method, but I can’t seem to get it. Any help? (By the way: I know user agent sniffing is no good, that’s why I’ll later back it with feature detection also.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T16:28:54+00:00Added an answer on June 12, 2026 at 4:28 pm

    You are reassigning $device often, blowing away what it previously had. By the time the script gets to the bottom, $device.desktop.windows no longer exists, because at that point $device is simply going to have type and no other properties.

    Instead of reassigning device each time, add to it:

    var $device = {
        mobile: {
            ...
        }
    };
    
    ...
    
    $device.mobile.any = $device.mobile.android || $device.mobile.blackberry || ... ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I want use html5's new tag to play a wav file (currently only supported
I need a function that will clean a strings' special characters. I do NOT
I am writing an app with both english and french support. The app requests
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.