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

  • SEARCH
  • Home
  • 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 8400115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:28:53+00:00 2026-06-09T21:28:53+00:00

I have an array of key:value pairs containing a keyword found in the useragent

  • 0

I have an array of key:value pairs containing a keyword found in the useragent string. I need to loop through these pairs and match each value for match to the navigator.userAgent string. I know browser sniffing is frowned upon but it’s not up to me. Instead of Ext.each (which I’m not all that familiar with) I can use a for loop. Here is the jsfiddle for how I went about solving this problem the first time
http://jsfiddle.net/tagZN/83/ but I was told to do this other way instead.

<script type="text/javascript"> 

var deviceProfiles = 
[ 
    '{match:Macintosh, name:Mac Desktop}',
    '{match:Windows NT, name:Windows Desktop}',
    '{match:Ubuntu, name:Ubuntu,layout:desktop}',
    '{match:Silk, name:Kindle Fire,layout:tablet}'

];     

var ua = navigator.userAgent;
var re = new RegExp(deviceProfiles.join("|"), "i");    
var identifyDevice = function( ua )
{
    Ext.each(
        deviceProfiles,
        function( profile )
        {
            return ua.match( profile.match ) == nil;
        }

    );
}    

</script>    

I was also originally given deviceProfile as (and told to fill in values for match & name):

var deviceProfiles =
[
    { 
        match: 'user agent regular expression here', 
        name: 'name of device here',        
    },

    { 
        match: 'user agent regular expression here', 
        name: 'name of device here'             
    },

    { 
        match: 'user agent regular expression here', 
        name: 'name of device here',            
    }
];​

but it didn’t work so I changed it to what you saw above. Was this a bad call. I am trying to decipher what I was given. I also created

var re = new RegExp(deviceProfiles.join("|"), "i");    

even though it’s not being used yet, I believe it will be necessary. I have tested this by setting ua equal to an actually user agent string that I copied and pasted where the keyword is contained in the array deviceProfiles and still no luck.

Any help is truly appreciated.

  • 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-09T21:28:54+00:00Added an answer on June 9, 2026 at 9:28 pm

    You are correct that browser sniffing is a flawed strategy for anything other than casual amusement.

    There are a number of issues with your code, not the least being that deviceProfiles is an array of strings, not objects. Also, the code is attempting to detect the user agent and then making a guess about the platform. It really has nothing to do with the device e.g. running IE in a virtual machine on a non-windows laptop returns “Windows desktop”, which is completely wrong. And what should be done with user agent strings that aren’t matched?

    There are thousands of browsers on devices like phones, laptops, game consoles and TVs. More devices access the internet and web pages every day, can you sniff all of them?

    Anyhow, here’s how to do what you are trying to do:

    function getDevice() {
      var deviceProfiles = [ 
          {match: 'Macintosh', name: 'Mac Desktop'},
          {match: 'Windows NT', name: 'Windows Desktop'},
          {match: 'Ubuntu', name: 'Ubuntu', layout:'desktop'},
          {match: 'Silk', name: 'Kindle Fire', layout:'tablet'}
      ];     
    
      var ua = navigator.userAgent;
      var profile, re;
    
      for (var i=0, iLen=deviceProfiles.length; i<iLen; i++) {
        profile = deviceProfiles[i];
    
        if (ua.match(profile.match)) {
          return profile.name;
        }
      }
    }
    
    alert(getDevice());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of objects with several key value pairs, and I need
I have an array that I need to add some key/value pairs too but
Say I have an array of key/value pairs in PHP: array( 'foo' => 'bar',
I have an array thats got about 12 potential key/value pairs. That are based
I want to loop a string as a key/value pairs. The data is given
I have data in the form of an array of key value pairs. Some
I have an array of JSON objects, some of which contain key/value pairs for
The problem is I have an array that has the key value pairs as
i have an array which could look like this: $config[$name][$array]['key'] = 'value'; // here
Does VBA have dictionary structure? Like key<>value array?

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.