开发者

How to download CSS file through JQuery

  • Is it possible to download a CSS file through JQuery?
  • Also, I need a callb开发者_如何学Goack which will notify me that the file is downloaded. The file will not be downloaded if the file exists in the client machine.

Please help with sample code. Thanks.


(function($) {
    $.get('/css/foobar/tooltips.css', function(data) {
        var style = document.createElement('style'),
            head  = document.getElementsByTagName('head')[0] || document.documentElemen;

        style.type = "text/css";
        style.textContent = style.text = data;

        head.appendChild(style, head.firstChild); 
    });
}(jQuery));

The above code would download the contents of tooltips.css. When the file is transfered, a new link node is created and the contents from tooltips.css are inserted. After that, the new node is appended into your document and all css rules will apply.

If you need/want to transfer many files dynamically, I would recommend a script like supplyJS


Adding more selector in your css and applying the change in the DOM will be simpler.

Local file access are restricted by the browser. This will probably lead to a big complexity of your code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜