Comments (6)
There's a way to do this for non-CDN usage... if the script is being self-hosted, the below can be used AFTER the inclusion of the dynamic polyfill script:
<script src="https://alink.to/promisepolyfill.js" onload="dynamicPolyfill([featuresarray], 'scripttouseURL', 'scriptfunctiontouse()', false)"></script>
Alternative (for CDN usage):
<script src="https://this.isalink.to/a/promisepolyfill.js"></script>
<script defer="defer" src="https://cdn.jsdelivr.net/gh/willstocks-tech/dynamically-polyfill-features-for-a-script@master/dynamicpolyfill.js" onload="dynamicPolyfill(['array', 'of.features', 'required'], 'scripttouseURL', 'scriptfunctiontouseafterpolyfillandload()', false)"></script>
from dynamically-polyfill-features-for-a-script.
callback
function??? Or the following dirty approach?
function dynamicPolyfill (features, scriptURL, initFunction) {
var polyfillFeatures = features;
var scriptToPolyfill = scriptURL;
var functionToRunonLoad = initFunction;
if(typeof Promise !== "undefined") {
return pageLoaded(polyfillFeatures, scriptToPolyfill, functionToRunonLoad);
} else {
console.warn("Promises not supported. Loading polyfill.");
var promPol = document.createElement('script');
promPol.src = ('https://polyfill.io/v3/polyfill.min.js?features=Promise');
document.body.appendChild(promPol);
polyfill.onerror = function(response) {
console.error("Could not load Promise polyfill", response);
}
polyfill.onload = function() {
return pageLoaded(polyfillFeatures, scriptToPolyfill, functionToRunonLoad);
}
}
}
from dynamically-polyfill-features-for-a-script.
@MilesZew mentioned in PR #15 (comment):
I am currently working on a promise polyfill incase you were planning on working on it.
Once confirmed, this issue will be assigned to him
from dynamically-polyfill-features-for-a-script.
Here is the unfinished file (for the Promise polyfill)
Polyfill.zip
from dynamically-polyfill-features-for-a-script.
Had a thought - rather than include an inline polyfill for promises, why not just check for native support first (I’m on mobile, so this is syntactically incorrect!):
if(Promise) { //check if natively supported
checkPolyfill(p1, p2, p3)
} else {
promifill(p1, p2, p3)
}
function promifill(p1, p2, p3) {
//polyfill promise here using polyfill.io
promifillScript.onload = function(p1, p2, p3) {
checkPolyfill(p1, p2, p3)
}
from dynamically-polyfill-features-for-a-script.
Changing milestone to v0.0.6 as this has now been catered for in PR #19
from dynamically-polyfill-features-for-a-script.
Related Issues (15)
- [Feature Request] Also allow array of "initialisation" functions! HOT 4
- [Review] Optimal performance & supportability HOT 3
- [Bug] Safari throws a bunch of errors initially HOT 2
- [Enhancement] checkNative if statement could be better
- [Bug] Chrome not executing init functions
- [Enhancement] Create a version of the script with inline comments
- [Bug] Polyfill.io is still being called when no polyfill required
- [Bug] Multiple polyfill.io requests rather than just one
- [Feature Request] typeOf item != ‘function’ missing as a native support check HOT 1
- [Enhancement] Code Cognitive Complexity HOT 1
- [Bug] Uglifying JS breaks... everything!
- [Enhancement] Further native feature checks
- Whole script needs to be a bit more "dynamic" HOT 3
- [Feature Request] Allow array of scripts to load HOT 1
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 dynamically-polyfill-features-for-a-script.