Published
Edited
Mar 12, 2021
1 star
Insert cell
Insert cell
Insert cell
// From https://observablehq.com/@mbostock/file-input
function getLoadedImage(src) {
return new Promise((resolve, reject) => {
const image = new Image();
image.onload = () => resolve(image);
image.onerror = reject;
image.src = src;
});
}
Insert cell
getLoadedImage(imgSrc)
Insert cell
Insert cell
function getLoadedIframe1(srcdoc) {
return new Promise((resolve, reject) => {
const iframe = html`<iframe>`;
iframe.onload = () => resolve(iframe);
iframe.onerror = reject;
iframe.srcdoc = srcdoc;
});
}
Insert cell
// It never resolves
getLoadedIframe1('<html><p>OK!</p></html>')
Insert cell
Insert cell
iframe2 = html`<iframe srcdoc="<html><p>OK!</p></html>">`
Insert cell
Insert cell
iframe2.contentWindow.document.location.href
Insert cell
Insert cell
new Promise((resolve, reject) => {
iframe2.addEventListener('load', () =>
resolve(iframe2.contentWindow.document.location.href)
);
})
Insert cell
Insert cell
awaitedIframe2 = new Promise((resolve, reject) => {
iframe2.addEventListener('load', () => resolve(iframe2));
})
Insert cell
awaitedIframe2.contentWindow.document.location.href
Insert cell
Insert cell
iframe3 = {
mutable calls3 = [];
return new Promise(resolve => {
const iframe = html`<iframe srcdoc="<html><p>OK!</p></html>">`;
document.body.appendChild(iframe);
iframe.onload = () => {
mutable calls3 = [
...mutable calls3,
iframe.contentWindow.document.location.href
];
document.body.removeChild(iframe);
resolve(iframe);
};
});
}
Insert cell
mutable calls3 = []
Insert cell
iframe3.contentWindow.document.location.href
Insert cell
Insert cell
iframe4 = {
const iframe = html`<iframe srcdoc="<html><p>OK!</p></html>">`;
yield iframe;
yield await new Promise((resolve, reject) => {
iframe.onload = () => {
resolve(iframe);
};
});
}
Insert cell
Insert cell
iframe4.contentWindow.document.location.href
Insert cell
Insert cell
{
mutable calls4 = [
...mutable calls4,
iframe4.contentWindow.document.location.href
];
}
Insert cell
mutable calls4 = []
Insert cell
Insert cell
function createIframe5() {
const iframe = html`<iframe srcdoc="<html><p>OK!</p></html>">`;
iframe.value = iframe;
iframe.onload = () => {
iframe.dispatchEvent(new CustomEvent('input'));
};
return iframe;
}
Insert cell
viewof iframe5 = createIframe5()
Insert cell
iframe5.contentWindow.document.location.href
Insert cell
mutable calls5 = []
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more