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 8999455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:10:58+00:00 2026-06-16T00:10:58+00:00

I have a small script that detects the userAgent and basically writes a small

  • 0

I have a small script that detects the userAgent and basically writes a small ‘hello’ message with the browser the user is using. I’m using it for a small demo i’ll be showing to people, I know feature detection is better so don’t worry. It’s mainly to demo the script.

But basically, I was wondering if there is any better way to write this:

http://jsfiddle.net/m5cVf/

I need to show a quick fun little message, but instead of doing loads of ‘if’ statements for each browser, I want to put them in array and add the browser name into the function if the current browser is true, rather than calling doAlert(); multiple times.

Something like:

var browsers = chrome,safari,mozilla;

then if the browser in use is ‘true’ this would get added into the function instead of manually adding them inside each ‘if’ statement. Thanks and much 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-16T00:10:59+00:00Added an answer on June 16, 2026 at 12:10 am

    You could store the browser types in an array, loop through them, and test them against the found user agent. Like this:

    http://jsfiddle.net/m5cVf/5/

    var browsers = [{"testWith": "chrome", "displayName": "Google Chrome"},
                    {"testWith": "safari", "displayName": "Apple Safari"},
                    {"testWith": "mozilla", "displayName": "Mozilla Browser"}];
    
    function showBrowserName(browser) {
        $('.user').text(browser);
    }
    
    $(function () {
        var userAgent = navigator.userAgent.toLowerCase();
    
        for (var i = 0; i < browsers.length; i++) {
            var cur = browsers[i];
            if (userAgent.indexOf(cur.testWith) > -1) {
                showBrowserName(cur.displayName);
                break;
            }
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this small PHP script that takes a POST variable and writes it
I currently have a small Python script that I'm using to spawn multiple executables,
I have this small script that checks if a user name and email exist
I have a small bash script that greps/awk paragraph by using a keyword. But
I have a small script that I am try to port to work for
I have a small script that reads a file. After reading a line i'm
I have a small cgi script that fetches and validates a configuration file for
I have a python script running on a small server that is called in
I have a small script which im using to test PHP mail(), as below:
I have this small script that sorts the content of a text file #

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.