Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8686453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:51:14+00:00 2026-06-12T22:51:14+00:00

I have seen some code to check if a background image on a div

  • 0

I have seen some code to check if a background image on a div is loaded. What they are doing is adding a img tag to memory but storing it in a variable and using that to see if the image is loaded with the load event. My question is does the $img tag stay in memory and how would I be able to remove that tag when the load event has been called.

  var $div = $('div'),
  bg = $div.css('background-image');
  if (bg) {
    var src = bg.replace(/(^url\()|(\)$|[\"\'])/g, ''),
      $img = $('<img>').attr('src', src).on('load', function() {
        // do something, maybe:
        $div.fadeIn();
      });
  }
});

I got this code above from this post

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T22:51:15+00:00Added an answer on June 12, 2026 at 10:51 pm

    You can remove elements with remove(). In your case the newly created <img> element is cached in the $img variable. So when you want to remove it you just do:

    $img.remove();
    

    To release the variable from memory you can use:

    delete $img;
    

    Although that isn’t really necessary if the variable scope is not global as the javascript garbage collector will take care of that for you.

    Also load() is deprecated and unreliable across browsers, so using it is discouraged. From jQuery docs:

    Caveats of the load event when used with images

    A common challenge developers attempt to solve using the .load()
    shortcut is to execute a function when an image (or collection of
    images) have completely loaded. There are several known caveats with
    this that should be noted. These are:

    • It doesn’t work consistently nor reliably cross-browser
    • It doesn’t fire correctly in WebKit if the image src is set to the same src as
    before
    • It doesn’t correctly bubble up the DOM tree
    • Can cease to fire for images that already live in the browser’s cache

    One alternative to load is this plugin.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen in some source code (by other developers) something like this: #import
I want to know the meaning of the code \. I have seen some
I have seen some code for working with AD in this stackoverflow question I
I have seen some code like this (not on consecutive lines, but in the
Have seen some similar questions: What is the difference between a JavaBean and a
I have seen some people in SO commenting that Singleton Pattern is an anti-pattern.
I have seen some related questions but none focusing on the specific problem I
I have seen some mails which has HTML content embedded in them. The content
I have seen some instances where people will say you have to use JS
i have seen some hand rolled solutions but does jquery out of the box

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.