Skip to content

Commit 0305f89

Browse files
committed
remove script from body on error
1 parent 02ce596 commit 0305f89

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

index.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@ function appendUnique(script, next) {
4949
* get a listener and when is loaded first and second will be completed.
5050
* @scenario 3: attempt load the same external script after is completed.
5151
*/
52-
function loadScript(args) {
53-
const src = args.src
54-
const id = args.id
52+
function loadScript({ src, id, data }) {
5553
const script = document.createElement('script')
5654

55+
script.id = id
56+
script.src = src
57+
script.setAttribute(`data-${data ? data : 'vendor'}`, id)
58+
5759
return new Promise((resolve, reject) => {
5860
// once the lib is registered you can resolve immediatelly
5961
// because it means that is fully loaded
@@ -68,13 +70,15 @@ function loadScript(args) {
6870
})
6971

7072
script.onerror = function onErrorLoadingScript() {
73+
// Remove the element from the body in case of error
74+
// to give the possibility to try again later
75+
// calling the same function
76+
document.body.removeChild(script)
7177
reject()
7278
}
7379

74-
script.id = id
75-
script.src = src
7680
appendUnique(script, resolve)
7781
})
7882
}
7983

80-
module.exports = loadScript
84+
export default loadScript

0 commit comments

Comments
 (0)