Created
August 10, 2020 14:09
-
-
Save ymqy/346c282ed6ad65ebc373991b582efd10 to your computer and use it in GitHub Desktop.
// source https://jsbin.com
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Document</title> | |
</head> | |
<body> | |
<script> | |
function loadImage(url) { | |
return new Promise((rs, rj) => { | |
const img = document.createElement('img'); | |
img.src = url; | |
img.onload = function() { | |
rs(url); | |
} | |
img.onerror = function() { | |
rj(new Error('img load failed.')); | |
} | |
}); | |
} | |
loadImage('https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png').then((value)=> { | |
console.log('value', value); | |
}, (err) => { | |
console.log('err', err); | |
});; | |
</script> | |
<script id="jsbin-source-html" type="text/html"><!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Document</title> | |
</head> | |
<body> | |
<script> | |
function loadImage(url) { | |
return new Promise((rs, rj) => { | |
const img = document.createElement('img'); | |
img.src = url; | |
img.onload = function() { | |
rs(url); | |
} | |
img.onerror = function() { | |
rj(new Error('img load failed.')); | |
} | |
}); | |
} | |
loadImage('https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png').then((value)=> { | |
console.log('value', value); | |
}, (err) => { | |
console.log('err', err); | |
});; | |
<\/script> | |
</body> | |
</html> | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment