Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image onload event after image has loaded

I have a script that attaches onload event handlers to all the images on a page, however because it's a GreaseMonkey script it can only run after the images are there, so some of them may have already loaded, is there a way to trigger the onload event if the image is already loaded?

like image 925
Bugster Avatar asked May 18 '11 15:05

Bugster


People also ask

How do you show image only when it is completely loaded?

Preload the image and replace the source of the <img /> after the image has finished loading. Show activity on this post. You can use the complete property to check if the image has finished loading.

Which event would you use to start a script after a page has been fully loaded by the browser?

The onload event occurs when an object has been loaded. onload is most often used within the <body> element to execute a script once a web page has completely loaded all content (including images, script files, CSS files, etc.).

What does IMG onload do?

Description. The onload property of an Image object specifies an event handler function that is invoked when an image loads successfully. The initial value of this property is a function that contains the JavaScript statements specified by the onload attribute of the <img> tag that defined the Image object.


1 Answers

If you're not adverse to using a framework such as jQuery, there's a triggerHandler() function that should do what you need.

You'll need a test to check whether or not the image has been loaded in your script (should be able to find details on how to do this using Google) - if it hasn't been loaded, simply attach the onload event handler; if it has been loaded, you can either attach the onload event handler and then trigger it using jQuery's triggerHandler() function, or execute the code/function that would have been executed by the onload event handler - which is easiest likely depends on what your onload event handler does.

like image 167
Anthony Grist Avatar answered Oct 16 '22 23:10

Anthony Grist