Skip to content

Commit dc81c5c

Browse files
committed
move src files to a new src folder. update paths respectively. update preload demo page
1 parent 7b4dd37 commit dc81c5c

File tree

7 files changed

+32
-95
lines changed

7 files changed

+32
-95
lines changed

README.md

+3-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Licensed MIT
66

77
## Usage
88

9-
Place the [`loadCSS` function](https://github.com/filamentgroup/loadCSS/blob/master/loadCSS.js) inline in the `head` of your page (it can also be included in an external JavaScript file if preferable).
9+
Place the [`loadCSS` function](https://github.com/filamentgroup/loadCSS/blob/master/src/loadCSS.js) inline in the `head` of your page (it can also be included in an external JavaScript file if preferable).
1010

1111
Then call it by passing it a stylesheet URL:
1212

@@ -46,7 +46,7 @@ By default, loadCSS will inject the new CSS stylesheet *after* the last styleshe
4646
4747
#### Using with `onload`
4848
49-
Onload listener support with `link` elements is spotty, so if you need to add an onload callback, include [`onloadCSS` function](https://github.com/filamentgroup/loadCSS/blob/master/onloadCSS.js) on your page and use the `onloadCSS` function:
49+
Onload listener support with `link` elements is spotty, so if you need to add an onload callback, include [`onloadCSS` function](https://github.com/filamentgroup/loadCSS/blob/master/src/onloadCSS.js) on your page and use the `onloadCSS` function:
5050
5151
``` javascript
5252
function onloadCSS( ss, callback ){ ... }
@@ -147,7 +147,7 @@ LoadCSS attempts to load a css file asynchronously, while maintaining the CSS ca
147147
<th>Yes</th>
148148
<th>No</th>
149149
</tr>
150-
150+
151151
</table>
152152

153153

@@ -161,5 +161,3 @@ The reason this script is sometimes necessary is because there is no cross-brows
161161
#### Contributions and bug fixes
162162

163163
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!
164-
165-

bower.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "loadcss",
33
"main": [
4-
"loadCSS.js"
4+
"src/loadCSS.js"
55
],
66
"ignore": [
77
"**/.*"

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "fg-loadcss",
33
"version": "0.2.4",
44
"description": "A function for loading CSS asynchronously",
5-
"main": "loadCSS.js",
5+
"main": "src/loadCSS.js",
66
"repository": {
77
"type": "git",
88
"url": "https://github.com/filamentgroup/loadCSS.git"

loadCSS.js src/loadCSS.js

File renamed without changes.

onloadCSS.js src/onloadCSS.js

File renamed without changes.

test/preload.html

+25-86
Original file line numberDiff line numberDiff line change
@@ -4,99 +4,38 @@
44
<title>Test for link[rel=preload]</title>
55
<meta charset="utf-8">
66

7-
8-
9-
<link rel="preload" href="http://scottjehl.com/css-temp/slow.php" as="stylesheet" id="asyncCSS" onload="this.rel='stylesheet'">
10-
7+
<link rel="preload" href="http://scottjehl.com/css-temp/slow.php" as="style" onload="this.rel='stylesheet'">
118
<script>
129

13-
// link rel=preload support test via https://lists.w3.org/Archives/Public/public-whatwg-archive/2015Apr/0013.html
14-
function preloadSupported() {
15-
var link = document.createElement('link');
16-
link.rel = 'PRELOAD';
17-
return link.rel == 'preload';
10+
/* CSS rel=preload polyfill */
11+
(function( w ){
12+
// rel=preload support test
13+
function support(){
14+
try {
15+
return w.document.createElement("link").relList.supports( "preload" );
16+
} catch (e) {}
1817
}
19-
20-
/*!
21-
loadCSS: load a CSS file asynchronously.
22-
[c]2015 @scottjehl, Filament Group, Inc.
23-
Licensed MIT
24-
*/
25-
(function(w){
26-
"use strict";
27-
/* exported loadCSS */
28-
var loadCSS = function( href, before, media ){
29-
// Arguments explained:
30-
// `href` [REQUIRED] is the URL for your CSS file.
31-
// `before` [OPTIONAL] is the element the script should use as a reference for injecting our stylesheet <link> before
32-
// By default, loadCSS attempts to inject the link after the last stylesheet or script in the DOM. However, you might desire a more specific location in your document.
33-
// `media` [OPTIONAL] is the media type or query of the stylesheet. By default it will be 'all'
34-
var doc = w.document;
35-
var ss = doc.createElement( "link" );
36-
var newMedia = media || "all";
37-
var ref;
38-
if( before ){
39-
ref = before;
40-
}
41-
else {
42-
var refs = ( doc.body || doc.getElementsByTagName( "head" )[ 0 ] ).childNodes;
43-
ref = refs[ refs.length - 1];
44-
}
45-
46-
var sheets = doc.styleSheets;
47-
ss.rel = "stylesheet";
48-
ss.href = href;
49-
// temporarily set media to something inapplicable to ensure it'll fetch without blocking render
50-
ss.media = "only x";
51-
52-
53-
// Inject link
54-
// Note: the ternary preserves the existing behavior of "before" argument, but we could choose to change the argument to "after" in a later release and standardize on ref.nextSibling for all refs
55-
// Note: `insertBefore` is used instead of `appendChild`, for safety re: http://www.paulirish.com/2011/surefire-dom-element-insertion/
56-
ref.parentNode.insertBefore( ss, ( before ? ref : ref.nextSibling ) );
57-
// A method (exposed on return object for external use) that mimics onload by polling until document.styleSheets until it includes the new sheet.
58-
var onloadcssdefined = function( cb ){
59-
var resolvedHref = ss.href;
60-
var i = sheets.length;
61-
while( i-- ){
62-
if( sheets[ i ].href === resolvedHref ){
63-
return setTimeout(cb);
64-
}
65-
}
66-
setTimeout(function() {
67-
onloadcssdefined( cb );
68-
});
69-
};
70-
71-
// once loaded, set link's media back to `all` so that the stylesheet applies once it loads
72-
if( ss.addEventListener ){
73-
ss.addEventListener( "load", function(){
74-
this.media = newMedia;
75-
});
18+
// loop preload links and fetch using loadCSS
19+
function poly(){
20+
var links = w.document.getElementsByTagName( "link" );
21+
for( var i = 0; i < links.length; i++ ){
22+
if( links[ i ].getAttribute( "rel" ) === "preload" ){
23+
w.loadCSS( links[ i ].href, links[ i ] );
24+
links[ i ].rel = null;
7625
}
77-
ss.onloadcssdefined = onloadcssdefined;
78-
onloadcssdefined(function() {
79-
if( ss.media !== newMedia ){
80-
ss.media = newMedia;
81-
}
82-
});
83-
return ss;
84-
};
85-
// commonjs
86-
if( typeof module !== "undefined" ){
87-
module.exports = loadCSS;
8826
}
89-
else {
90-
w.loadCSS = loadCSS;
91-
}
92-
}( typeof global !== "undefined" ? global : this ));
93-
94-
95-
27+
}
9628
// if link[rel=preload] is not supported, we must fetch the CSS manually using loadCSS
97-
if( !preloadSupported() ){
98-
loadCSS( asyncCSS.href );
29+
if( !support() ){
30+
poly();
31+
var run = w.setInterval( poly, 300 );
32+
if( w.addEventListener ){
33+
w.addEventListener( "load", function(){
34+
w.clearInterval( run );
35+
} )
36+
}
9937
}
38+
}( this ));
10039

10140
</script>
10241
</head>

test/qunit/index.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
<link rel="stylesheet" href="./libs/qunit/qunit.css" media="screen">
99
<script src="./libs/qunit/qunit.js"></script>
1010
<!-- Load local lib and tests. -->
11-
<script src="../../loadCSS.js" id="loadCSS"></script>
12-
<script src="../../onloadCSS.js"></script>
11+
<script src="../../src/loadCSS.js" id="loadCSS"></script>
12+
<script src="../../src/onloadCSS.js"></script>
1313
<script src="./tests.js"></script>
1414
</head>
1515
<body>

0 commit comments

Comments
 (0)