Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fg-loadcss

Package Overview
Dependencies
Maintainers
2
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fg-loadcss

A function for loading CSS asynchronously

  • 0.2.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
54K
decreased by-14.38%
Maintainers
2
Weekly downloads
 
Created
Source

loadCSS

A function for loading CSS asynchronously [c]2015 @scottjehl, Filament Group, Inc. Licensed MIT

Usage

Place the loadCSS function inline in the head of your page (it can also be included in an external JavaScript file if preferable).

Then call it by passing it a stylesheet URL:

<head>
...
<script>
  // include loadCSS here...
  function loadCSS( href, before, media ){ ... }
  // load a file
  loadCSS( "path/to/mystylesheet.css" );
</script>
<noscript><link href="path/to/mystylesheet.css" rel="stylesheet"></noscript>
...
</head>

By default, loadCSS will inject the new CSS stylesheet after the last stylesheet or script in the page. This should retain your CSS cascade as you'd expect.

Optional Arguments
  • before: By default, loadCSS attempts to inject the stylesheet link after all CSS and JS in the page. However, if you desire a more specific location in your document, such as before a particular stylesheet link, you can use the before argument to specify a particular element to use as an insertion point. Your stylesheet will be inserted before the element you specify. For example, here's how that can be done by simply applying an id attribute to your script.
... ... ```
  • media: You can optionally pass a string to the media argument to set the media="" of the stylesheet - the default value is all.
Using with onload

Onload listener support with link elements is spotty, so if you need to add an onload callback, include onloadCSS function on your page and use the onloadCSS function:

function onloadCSS( ss, callback ){ ... }

var stylesheet = loadCSS( "path/to/mystylesheet.css" );
onloadCSS( stylesheet, function() {
	console.log( "Stylesheet has asynchronously loaded." );
});

Browser Support

LoadCSS attempts to load a css file asynchronously, while maintaining the CSS cascade, in any JavaScript-capable browser. However, some older browsers will block rendering while the stylesheet is loading. This table outlines css loading support and async loading support.

BrowserCSS Loads SuccessfullyCSS Loads without Blocking Render
Chrome Mac (latest and many recent versions)YesYes
Firefox Desktop (latest and many recent versions)YesYes
Opera Mac (latest and many recent versions)YesYes
Safari Mac (latest and many recent versions)YesYes
Safari iOS (latest and many recent versions)YesYes
Chrome Android 5.xYesYes
Chrome Android 4.xYesYes
Android Browser 2.3YesNo
Kindle Fire HDYesYes
Windows Phone IE 8.1YesYes
IE 11YesYes
IE 10YesYes
IE 9YesYes
IE 8YesNo
IE 7YesNo
IE 6YesNo

Usage Tips

We typically use loadCSS to load CSS files that are non-critical to the initial rendering of a site. See the EnhanceJS project Readme for examples of how we typically use it to improve page loading performance.

The reason this script is sometimes necessary is because there is no cross-browser means in HTML (currently at least) to load and apply a CSS file asynchronously. CSS references that use either link or import will cause browsers to block page rendering by default while their related stylesheet loads.

Contributions and bug fixes

Both are very much appreciated - especially bug fixes. As for contributions, the goals of this project are to keep things very simple and utilitarian, so if we don't accept a feature addition, it's not necessarily because it's a bad idea. It just may not meet the goals of the project. Thanks!

FAQs

Package last updated on 21 Oct 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc