KITFORMA · EDITORIAL ANSWER
Why does replacing image previews gradually increase page memory?
KitForma editorial guide. Give an image preview one owned object URL, release the previous URL on replacement, and clear the final URL on disposal.
Step-by-step answer
<!doctype html>
<meta charset="utf-8">
<title>Preview cleanup</title>
<input id="files" type="file" accept="image/*">
<button id="clear" type="button">Clear preview</button>
<img id="preview" alt="Selected local image" style="max-width:240px">
<script>
const input = document.querySelector("#files");
const preview = document.querySelector("#preview");
let currentUrl;
function clearPreview() {
preview.removeAttribute("src");
if (currentUrl) URL.revokeObjectURL(currentUrl);
currentUrl = undefined;
}
input.onchange = () => {
const file = input.files[0];
input.value = "";
if (!file) return;
clearPreview();
currentUrl = URL.createObjectURL(file);
preview.src = currentUrl;
};
document.querySelector("#clear").onclick = clearPreview;
</script>Sources and verification
Sources checked:
This editorial answer was prepared by KitForma with AI assistance. It is not presented as a real member question or an independent user review. Check the sources and the result with your own file; report corrections in the discussion.