Will this ALWAYS be in the DOM at this point? If not, wrap with an "if (img.parentNode)"