tags:

views:

42

answers:

3

I have made a jQuery plugin.

But it doesn't work properly. Sometimes it does work, but sometimes not.

I think it's some of the variables that doesn't load fast enough...

Here's the source: http://pastebin.com/h4tfCWNM

Hope someone can help

Thanks in advance

EDIT The plugin is used for my image uploader, when i upload an image it should appear in a fancy way. I use this plugin for uploading: valums.com/ajax-upload And onComplete i use my plugin to show the image.

A: 

I've made some further investigation, and i have concluded the 2 variables elementWidth and elementHeight doesn't load fast enough. How can i ensure they are loaded before i continue running the script?

Mikkel
That really makes no sense; the jQuery routines you're calling to get the width and height are synchronous and will return values to you immediately.
Pointy
When i insert some static integers it works as it should, but when i use jQuery to get the data it doesn't...
Mikkel
+1  A: 

Correct me if I'm wrong:

  1. An image upload form, using AJAX Upload, allows users to upload an image onto your server.
  2. AJAX Upload calls an onComplete callback with the URI of the newly-uploaded image.
  3. You create a new img DOM element with src set to the URI of the uploaded image.
  4. The img DOM element is added to the document.
  5. showhide is called on the img element.

If this is correct, then it explains why elementWidth and elementHeight are sometimes not correct. The problem is that the browser needs time to download the image, and elementWidth and elementHeight are not valid until the image has been fully loaded.

To fix this problem, I would try calling showhide in a load callback on the img that is registered before the src attribute is set:

var     $myNewImage = $('<li><img alt="" /></li>'),
        $i = $myNewImage.children('img');

$i.hide();
$myNewImage.rightClick(function(e) {
        // ....
});

$myNewImage.prependTo('.uploadedImages');
$('.uploadedImages li').each(function() {
        var indexNumber = $(this).index();
        $(this).children('img').attr('id', 'image_' + indexNumber);
});

$i.load(function() {
        $i.showhide({ appear: true });
});
$i.attr('src', 'uploads/thumbs/' + file);
Daniel Trebbien
A: 

@Daniel Trebbien Actually my code is a bit more advanced than that: http://pastebin.com/XQKGzX2j I can't figure out how to implement your code

Mikkel
I figured it out
Mikkel
I can't mark your answer as correct. I think it's because i registered my guest user...
Mikkel
@Mikkel: I updated my answer. You don't have enough reputation to upvote; however, you should be able to click a check mark next to my answer: http://meta.stackoverflow.com/questions/5234/accepting-answers-whats-it-all-about
Daniel Trebbien
It's not visible, StackOverflow doesn't see me as "Mikkel" the guest user. Btw thanks for helping with the code
Mikkel
@Mikkel: Okay. That's fine. Does your code work now?
Daniel Trebbien
It's working perfectly. Thank you very much
Mikkel
Oh good. You're welcome.
Daniel Trebbien