Created
August 31, 2022 10:08
-
-
Save ankit-kumar-jat/160bab4d192020005804d3313882ca4d to your computer and use it in GitHub Desktop.
Load javascript file dynamically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* @param {String} url scrip url | |
* @param {String} id id for script tag | |
* @returns promise | |
*/ | |
const loadScript = (url, id) => { | |
return new Promise((resolve) => { | |
const existingScript = document.getElementById(id); | |
if (existingScript) resolve(true); | |
const script = document.createElement('script'); | |
script.src = url; | |
script.id = id; | |
script.type = 'text/javascript'; | |
script.onload = () => { | |
resolve(true); | |
}; | |
script.onerror = () => { | |
resolve(false); | |
}; | |
document.body.appendChild(script); | |
}); | |
}; | |
export default loadScript; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment