Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to write a code in javascript that conditionally loads 2 different css files?

how to write a code in javascript that conditionally loads 2 different css styles I need to load a different css style for a page while it is loading and after it has loaded. i.e

if(condition)
{load 1 css};
else 
{load another css};
like image 713
TRIPTI RAWAL Avatar asked Nov 27 '25 04:11

TRIPTI RAWAL


1 Answers

You should be aware of the <link> tag's disabled feature.

<link rel="stylesheet" disabled="disabled" />

So, in your case, let us assume there are two stylesheets, day.css and night.css:

<link rel="stylesheet" href="day.css" id="day" />
<link rel="stylesheet" href="night.css" id="night" />

In your code, once you load, you can do this:

if (condition)
    document.getElementById('day').setAttribute("disabled", "disabled");

In case, you are using jQuery, it is more simpler! Use this:

if (condition)
{
    $("#day").prop("disabled", true);
    $("#night").prop("disabled", false);
}
else
{
    $("#day").prop("disabled", false);
    $("#night").prop("disabled", true);
}
like image 58
Praveen Kumar Purushothaman Avatar answered Nov 29 '25 17:11

Praveen Kumar Purushothaman