kennethcachia / background-check

Automatically switch to a darker or a lighter version of an element depending on the brightness of images behind it.
http://kennethcachia.com/background-check/
MIT License
3.27k stars 282 forks source link

The script breaks if it fails working with an element #55

Open isaacalves opened 9 years ago

isaacalves commented 9 years ago

I've got the following message:

Uncaught Element is not an but does not have a background-image

That doesn't necessarily had to make the script break, because there were other elements on the page with that class for background-check to check.

avenir commented 9 years ago

Yup same error for me:

"Uncaught Element is not an <img> but does not have a background-image"

In my case the script breaks. I assume it's some kind of conflict with other scripts on the site as it works on the blog section. I'll report back if I track the conflict down.

dagomar commented 9 years ago

I had the same issue. It is because the script throws an error if it doesn't find the elements. However in my case I just don't care if it doesn't find anything, I don't want that all my scripts suddenly stop working because of this. I adjusted line 167 and 189 to not throw an error. (just commented the lines with 'throw' out). Perhaps it should only inform the user when debug is enabled? Are there any side effects in removing the throw declarations?

princeofnaxos commented 8 years ago

Since it is quite impossible to always have all elements in all pages of a website, the script should definitely not throw "Uncaught" errors. Just quietly ignore the "not found" elements seems better.

oddpixel commented 8 years ago

I got around this by checking for the class name that triggers the script before initializing it on a page. Definitely agree that it should just fail gracefully if nothing is found.

if( $('.bgcheck')[0] ) {
    BackgroundCheck.init({
        targets: '#back-to-top',
        images: '.bgcheck',
        threshold: 70
    });
}
sasoriza commented 7 years ago

I had the same problem and could only fix it by adding the background-image property inlined instead of declaring it in my stylesheet