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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:36:18+00:00 2026-05-26T12:36:18+00:00

My web application target to major Smartphones and I need to change the CSS

  • 0

My web application target to major Smartphones and I need to change the CSS file according to device (if there are issues in the UI need to hit them), and I’m planning swap CSS using following jQuery. Just want to know whether is it a best practice and good in performance?

<link rel="stylesheet" href="basic.css" type="text/css" class="cssLink" />
<link rel="stylesheet" href="general.css" type="text/css" />

<script type="text/javascript"> 
    $(document).ready(function() {

        //  css file based on the device
        var controlCss;
        //  get the device agent and conver to lover case
        var deviceAgent = navigator.userAgent.toLowerCase();

        if(deviceAgent.match(/android/i)){
            controlCss = "android.css";
            $(".cssLink").attr("href", controlCss);
        }
        else if(deviceAgent.match(/webso/i)){
            controlCss = "webOS.css";
            $(".cssLink").attr("href", controlCss);
        }
        else if(deviceAgent.match(/iphone/i)){
            controlCss = "iphone.css";
            $(".cssLink").attr("href", controlCss);
        }
        else if(deviceAgent.match(/ipod/i)){
            controlCss = "ipad.css";
            $(".cssLink").attr("href", controlCss);
        }
        else if(deviceAgent.match(/blackberry/i)){
            controlCss = "bb.css";
            $(".cssLink").attr("href", controlCss);
        }
        else {
            controlCss = "basic.css";
            $(".cssLink").attr("href", controlCss);
        }
    }); 
</script>
  • 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-05-26T12:36:19+00:00Added an answer on May 26, 2026 at 12:36 pm

    1.Is it best practice?

    Depends on what you think of as best practice, also what best practice is in the context of your application and your company. One thing this makes me think about is: Can you guarantee all your pages will be using jQuery? If so then I think this is a good approach to achieve what you are after. An alternative would be to do this server-side, that would guarantee best-performance but there may be other reasons why you dont want to do this (maybe you dont have access to server-side code, or you want to maintain most of the functionality in the hands of front-end programmers).

    2.Is it good in performance?

    The short answer is no. On top of needing the 100K+ payload of jQuery to inject the CSS on the page. The way you’ve approached the problem at the moment is to wait for the whole page (and all dependencies) to load before adding styles to it. This will create a noticeable ‘jump’ between when the page gets displayed at first (without styles) and when the styles get loaded and everything moves around.

    Loading the CSS server-side will get rid of this, but I think you can still do this in the UI and keep the majority of your code-base in JavaScript which will make it easier to maintain. In order to do this, remove the bit where you wait for the document to be loaded before calling up your CSS file:

    <link rel="stylesheet" href="basic.css" type="text/css" class="cssLink" />
    <link rel="stylesheet" href="general.css" type="text/css" />
    
    <script type="text/javascript"> 
    
         // No need to wait for document to load
         // $(document).ready(function() {
    
            //  css file based on the device
            var controlCss;
            //  get the device agent and conver to lover case
            var deviceAgent = navigator.userAgent.toLowerCase();
    
            if(deviceAgent.match(/android/i)){
                controlCss = "android.css";
                $(".cssLink").attr("href", controlCss);
            }
    
            // etc..
    
        // }); 
    </script>
    

    To further improve performance you could use a solution that does not depend on jQuery, instead of

    $(".cssLink").attr("href", controlCss);
    

    you could add #cssLink to the stylesheet <link> element and use the DOM to do the same:

    document.getElementById("cssLink").setAttribute("href", controlCss);
    

    This would make you code look as follows:

    <link rel="stylesheet" href="basic.css" type="text/css" css="cssLink" id="cssLink" />
    <link rel="stylesheet" href="general.css" type="text/css" />
    
    <script type="text/javascript"> 
    
            // .. blah blah .. 
    
            if(deviceAgent.match(/android/i)){
                controlCss = "android.css";
                // use a solution that does not need jQuery
                document.getElementById("cssLink").setAttribute("href", controlCss);
            }
    
            // etc..
    
    </script>
    

    This way you will remove the dependency on the 100K plus payload of jQuery before you can apply your stylesheets to the page.

    UPDATE:

    It is also possible to apply CSS rules based on screen size rather than device.

    Have you had a look at @media queries?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My web application as a topbar where i need to display the number of
I have an ASP.NET Web Application project that used to target ASP.NET 2.0. I
I want to provide 'install' target for Makefile for web application. I'd like to
I am working on a web application that will target modern browsers or recent
I have a .NET 3.5 (target framework) web application. I have some code that
I have a large .NET 2 web application that has been updated to target
I have developed a web application in asp.net, there is a page in this
I have a web application originally designed to target IE7. I am readjusting the
I have a web application that issues requests to 3 databases in the DAL.
I set the Target Framework for a Web Application to .Net Framework 3.5, but

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.