Dynamic CSS insertion with Javascript

最近碰到的需求,在沒有 server side script supported 的前提下,動態載入 CSS。

function appendCSS()
{
var headID = document.getElementsByTagName("head")[0];
var cssNode = document.createElement("link");

cssNode.type = "text/css";
cssNode.rel = "stylesheet";
cssNode.media = "screen";
cssNode.href = "css/style.css";

headID.appendChild(cssNode);
}

只要再搭配個判斷 browser 版本的 function 就可以簡單實現根據不同的 browser 切換對應 CSS 的目的了。

Comments

Popular posts from this blog

How to show only month and year fields in android Date-picker?

How to construct a B+ tree with example

Conflict Serializability in database