user2507818
user2507818

Reputation: 3037

issue with $(window).load() events in jQuery

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script type="text/javascript"> 
$(window).load( function(){ console.log('Loading finished.'); } ); 
console.log($(window).load);
</script>  

In chrome->console, it shows:

function (e,n,r){if("string"!=typeof e&&Sn)...
Loading finished.

Question:

  1. In above scripts, this line:$(window)... is at the top, why its output is not at the beginning?

  2. Actually I want to check the handler in the event load, it should be function(){ console.log('Loading finished.'); } so:

a. how to output the event handler?

b. what does the output(function (e,n,r)...) mean?

Upvotes: 1

Views: 105

Answers (3)

Hitesh Siddhapura
Hitesh Siddhapura

Reputation: 1392

 $(window).load( function(){ console.log('Loading finished.'); } ); 

Above statement bind your function with Window Load event, so when Window load, your function will run.

console.log($(window).load);

Above statement print output into console, what is $(window).load, like this

$(window)

is object which represent Window, and

.load

is event/function of window object, so output is (function (e,n,r)...). In simple language, load is function which takes 3 arguments

Hope you understand

Upvotes: 0

A. Wolff
A. Wolff

Reputation: 74420

You can use the $._data events array like that: {in older jquery version it is $.data}

DEMO

var dataLoad = $._data(window, 'events').load;
for (var i = 0, z = dataLoad.length; i < z; i++)
console.log($._data(window, 'events').load[i].handler);

Upvotes: 2

mohkhan
mohkhan

Reputation: 12305

In above scripts, this line:$(window)... is at the top, why its output is not at the beginning?

That is because you have just binded the event listener to the window variable. The load function is not called instantaneously, but it is called when the browser finishes loading the complete HTML. Your second statement is not an event, hence it gets executed immediately and thus you see the output.

what does the output(function (e,n,r)...) mean?

When you print a function object to the console, JS will strigify the entire function and its body.

a. how to output the event handler?

I really didn't get what you want here.

Upvotes: 1

Related Questions