-1

I'm looking for a way to include css files through variable in html file.

<html>
  <head>
    <!-- variable = "/css/style.css" -->
    <link rel="stylesheet" href=variable>
    <link rel="stylesheet" href=variable>
  </head>
  <body>
    <!-- Do anything -->
  </body>
</html>
demavend
  • 103
  • 1
  • 1
  • 5

2 Answers2

1

You can do that using JavaScript ... Create a JavaScript variable and according to its value change the value of the href attribute something like this

<link href="url.css" id="selector" type="text/css" />
<script> var variable = 'value`;  </script>
...
<script>
if (variable) {  // Or whatever you need to do
  document.getElementById("selector").href = "another.css";
}
</script>
Ahmad Alfy
  • 13,107
  • 6
  • 65
  • 99
0

This also works....

 <script type="text/javascript">
                    var target = document.createElement("script");
                    target.type = "text/javascript";
                    target.src = "script/knockout.js";
                    document.getElementById('head').appendChild(target);

                    var cssTar = document.createElement("link");
                    cssTar.rel = "stylesheet";
                    cssTar.type = "text/css";
                    cssTar.href = "Scripts/StyleSheet1.css";
                    document.getElementById('headid').appendChild(cssTar);
           </script>
Bindrid
  • 3,655
  • 2
  • 17
  • 18