Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

154
Views
Jquery each, how to not take broken urls?

How to not take images with broken url?

jQuery.each(jQuery('img'), function(k, o) {
imageStack.push(jQuery(o));
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Only push the images whose load event has fired to imageStack.

To do this, you can bind a load event listener to every image which pushes the image to imageStack.

const imageStack = []

$('img').on('load', function() {
  imageStack.push($(this));
})

$(window).on('load', function(){
 //wait for every image to load, so imageStack will contain every loaded image
  console.log(imageStack)
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img src="https://www.gravatar.com/avatar/0fdacb141bca7fa57c392b5f03872176?s=48&d=identicon&r=PG&f=1">
<img src="brokenurl">

Alternatively, you can wait for every image to load by listening for the window load event, then check the image's naturalWidth property:

const imageStack = []

$(window).on('load', function() {
  $('img').each(function() {
    if (this.naturalWidth != 0) {
      imageStack.push($(this));
    }
  })
  console.log(imageStack)
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img src="https://www.gravatar.com/avatar/0fdacb141bca7fa57c392b5f03872176?s=48&d=identicon&r=PG&f=1">
<img src="brokenurl">

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!