Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Simple LightWeight function to defer css in your web applications or websites.
DEMO: Jsfiddle.net
Two functions and one object is set to global window
Function | Arguments | Usage |
---|---|---|
deferCss | (scripts=[], mountOnId='defer-css') | For loading a style or array of styles |
hasStyleSheet | (search='', return=false) | A bonus helper function for checking if the browser has loaded a particular stylesheet using document.styleSheets |
if $return is "all" the CSSStyleSheet Object is returned, else if any string we assume its a key in the CSSStyleSheet object you are trying to get. |
<!--Using JsDeliver CDN-->
<script src="https://cdn.jsdelivr.net/npm/defer-css"></script>
<!-- Or Using UnPkg CDN-->
<script src="https://unpkg.com/defer-css"></script>
You can include defer-css
in your project using require
or import
but defer-css does not export anything.
It sets window.deferCss
, window.deferCssData
&& window.hasStyleSheet
Defined styles are loaded before any element with id defer-css
id in your page
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/defer-css"></script>
<!-- Styles will be placed before this element-->
<link id="defer-css"/>
</head>
</html>
You can change this to your custom id
<head>
<!-- Some Styles -->
<link href="....">
<link href="....">
<link href="....">
<!-- Styles will be placed before this add-css-here element-->
<link id="add-css-here"/>
</head>
Load Css using javascript
deferCss([
'style-1.min.css',
{href: 'style-2.min.css', crossOrigin: 'anonymous'}
], 'add-css-here')
Adds the following styles
<link rel="stylesheet" href="style-1.min.css">
<link rel="stylesheet" href="style-2.min.css" crossorigin="anonymous">
The values of the styles array can either be a url string
or an object
that will be used to build your link
element
If the object includes an onDefer
function, it is executed when the css file is loaded.
deferCss([
{
href: 'style-1.min.css',
onDefer: function() {
// do something
}
},
])
Lets say you want to mount css in multiple places.
<head>
<link id="main-css">
<style>
.some-style-before-other-css{
background: teal;
}
</style>
<link id="other-css">
</head>
deferCss(['main-css-1.css', 'main-css-2.css'], 'main-css');
deferCss([
{ href: 'other-css-1.css', crossOrigin: 'anonymous' },
'other-css-2.css'
], 'other-css');
This will result to.
<link rel="stylesheet" href="main-css-1.css">
<link rel="stylesheet" href="main-css-2.css">
<style>
.some-style-before-other-css{
background: teal;
}
</style>
<link rel="stylesheet" href="other-css-1.css" crossorigin="anonymous">
<link rel="stylesheet" href="other-css-2.css">
The deferCssData
includes details you may need.
({
// Element mounted on, default = 'defer-css'
"defer-css": {
total: Number, // total number of css defined
loaded: Number // total number of css loaded (at the moment)
}
});
FAQs
ExtraSlim Javascript to Load css in your page without affecting load speed.
The npm package defer-css receives a total of 5 weekly downloads. As such, defer-css popularity was classified as not popular.
We found that defer-css demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.