2

I have two JavaScript functions that I want to call from inside of a jQuery function. Because I load images dynamically, I want to check the width of each image by calling the two functions func1() and func2() that will check the width.

If width < 20px then double width

I don't want to wait for the images to fully load then do the check.

I tried this but didn't work

$(document).ready(function ()
{
    $('img').each(function()
    {
       ....
    })

    func1();
    func2();
})

function func1()
{
   ....
}

function func2()
{
   ....
}

What am I missing?

9
  • 4
    Semicolons after the ready/each calls. What does the browser error console show? Commented Dec 18, 2011 at 13:56
  • This code would call func1() and func2() as soon as the DOM is ready. What is it that you want to do? Commented Dec 18, 2011 at 13:57
  • 1
    $('body').each is kind of weird - you only have one body. Commented Dec 18, 2011 at 13:57
  • Are you sure you have included jQuery? It works: jsfiddle.net/37YKR Commented Dec 18, 2011 at 13:59
  • @tvanfosson error console shows nothing. Commented Dec 18, 2011 at 14:04

3 Answers 3

1

Unless the img tags have the width specified you will not be able to correctly get the width before it is completely loaded. The browser has no way of knowing what are the dimensions until it has the entire image.

Here is a way to wait until the images are loaded:

Official way to ask jQuery wait for all images to load before executing something

Sign up to request clarification or add additional context in comments.

Comments

0

I had a problem similar your issue. I think you have to move your functions into jquery block.

$(document).ready(function() {
    function1 () {
       ...       
    }
    function2 () {
       ... 
    }
});

Otherwise, I don't think they can be called from inside of jQuery. Test it and inform me about the result.

2 Comments

no need to put inside jQuery block. it is always recommend to put outside of document ready event. otherwise functions are evaluating only after page load. this will increase page load time
Thank you Chamika. I'll try what you said.
0

You could change this:

$(document).ready(function ()

to this:

$(window).load(function ()

to ensure that all images are loaded.


Another option would be to bind load handlers to the images themselves, but there may be issues if the images are cached.

$('img').load(function() {
    func1();
    func2();
});

You should note that this will call the functions once for each image, which may mean that you'll need to change how the functions work. Like have them receive the individual image as an argument.

1 Comment

:( using that has the same effect as window.onload = messWH; which is not I want. Guess it's like what epignosisx said. But hey thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.