1

The source code adds a load event handler to an element using JQuery. I am testing this using JSTestDriver.

The code looks something like this:-

       $(this).load(function () {
           alert("Foo");
       });

When I run the tests the alert("Foo") never happens. If I change it to:-

       $(this).ready(function () {
           alert("Foo with Ready");
       });

The test works.

If however, the source code is executed separately within an HTML, then it runs fine and I can get the alert("Foo") even when the event is "load".

Is this a bug in JSTestDriver or am I doing something incorrectly? Do, I have to wait till all the event handlers get triggered in the unit tests. (Something on the lines of Thread.join()) ?

-Ajay

user855
  • 19,048
  • 38
  • 98
  • 162

2 Answers2

0

not sure why the load event is not being binded. an even better question is why jquery has two different methods with the same name (load() to bind an event, and load() to load some ajaxed content). here is an alternative syntax however that you migth want to try.

$(this).bind('load', handler);

if this works for you, please select the check mark next to this answer. i am trying to get enough rep to leave comments haha. thanks!

also, where is your binding code? is it possible that you are binding the event after the image has already loaded?

dqhendricks
  • 19,030
  • 11
  • 50
  • 83
0

I believe your problem is not in the event binding, but in the meaning of $(this) in your code.

Could we get a broader scope?

One way to test would be to replace the $(this) with the actual selector for the element you are trying to trigger the event with.

or even right before the event binding, make this call to make sure the id of the element matches the id of the intended element.

alert($(this).attr('id'));
jondavidjohn
  • 61,812
  • 21
  • 118
  • 158