Comments (6)
Here is a variant if you have multiple styles
<script nonce> Array.prototype.slice.call(document.querySelectorAll("[data-loadcss]")).map(function (e) { e.addEventListener("load", function() { this.media = "all" }); }) </script>
or shorter es6 syntax
<script nonce> [...document.querySelectorAll("[data-loadcss]")].map((e) => e.addEventListener("load", (e) => e.target.media = "all")); </script>
Sorry for necromancing an old issue with a (unrelated) question.
But using the es6/other variant like just including it to the bottom will async/inline onload but not cause csp to break?
Like no adjustments needed? Just chug it to the bottom? Or should it be placed in the header?
Again, sorry for reviving an issue with a unrelated (noob) question. I do look forward to your reply!
I've tried to implement it here: https://github.com/TheVoxSiren/voxsiren.net/blob/main/index.html#L7
But i feel like it's not really doing anything?
from loadcss.
Looks good! Thanks for noting. It should work the same yes, but I'm not sure if there's a possibility of the CSS loading from cache before the onload event is bound. It does make me wonder if a timeout might be more dependable, since it's not onload
in particular that needs to be used so much as some event that happens after the CSS file is requested.
Worth testing.
Anyway, I'd love a readme edit if you'd like to. Otherwise we'll aim to get to it when we can.
from loadcss.
Nice find & workaround @petervtzand ! One question though, why don't you "activate" the stylesheet in the link by setting the rel-attribute to "stylesheet" rather than changing the media attribute?
from loadcss.
I've used this example of the filament group: https://www.filamentgroup.com/lab/load-css-simpler/. This seems to be their new and simpler approach.
from loadcss.
Here is a variant if you have multiple styles
<script nonce>
Array.prototype.slice.call(document.querySelectorAll("[data-loadcss]")).map(function (e) {
e.addEventListener("load", function() {
this.media = "all"
});
})
</script>
or shorter es6 syntax
<script nonce>
[...document.querySelectorAll("[data-loadcss]")].map((e) => e.addEventListener("load", (e) => e.target.media = "all"));
</script>
from loadcss.
Btw, I've noticed a weird bug where event listener did not trigger in Edge 18. So I've had to add this workaround. Works with onchange though, weird.
if (navigator.userAgent.match("Edge/")) {
e.media = "all";
}
from loadcss.
Related Issues (20)
- FOUC on 'No Throttled' condition? HOT 2
- Polyfill has an issue in chrome
- Optionally preload without as attr?
- Don't rely on invalid media attributes to download? HOT 10
- standalone or via npm?
- element with id `global` prevents loadCSS execution
- Error thrown by Content-Security-Policy HOT 5
- Supports preload
- The plugin doesn't do anything to preload elements in Firefox HOT 1
- Cannot install version 3.0.0 because node version is hardcoded to 11.9.0 HOT 5
- Where has the cssrelpreload polyfill gone in 3.0.0? HOT 2
- Required node version HOT 5
- Export onloadCSS
- CSS is being loaded twice HOT 3
- loadCSS not helping with render-blocking content
- Why do I sometimes get a recommendation in google pagespeed 'Reduce unused CSS' ? HOT 1
- Wouldn't loading css asynchronously cause FOUC? HOT 1
- Calling loadCSS from a TypeScript Vite application throws Uncaught TypeError: Cannot read properties of undefined (reading 'createElement') at loadCSS (loadCSS.js:13:16)
- Help - loadCSS/Critical file load not working
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from loadcss.