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

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more