Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: What argument does the function specified in body's onload event get called with?

Similar to this question, my HTML looks like this:

<body id="body" onload="loader()">
</body>

I always assume, as this doc says, that onload is given no arguments. However, I named the argument, and did some deep inspection, and found that I got an object looking like this:

{originalTarget : DOM, 
preventCapture : function, 
target : DOM, 
cancelable : Bool, 
currentTarget : DOM, 
timeStamp : Int, 
bubbles : Bool, 
type : String, 
eventPhase : Int, 
preventDefault : function, 
initEvent : function, 
stopPropagation : function, 
CAPTURING_PHASE : Int, 
AT_TARGET : Int, 
BUBBLING_PHASE : Int, 
explicitOriginalTarget : DOM, 
preventBubble : function,
isTrusted : Bool, 
MOUSEDOWN : Int, 
MOUSEUP : Int, 
MOUSEOVER : Int, 
//... (more constants)
}

Anyone have any idea what that thing is, or what its classname might be?

like image 532
Claudiu Avatar asked Feb 19 '10 00:02

Claudiu


People also ask

How do you call an onload function in body tag?

The onload attribute fires 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.). However, it can be used on other elements as well (see "Supported HTML tags" below).

Is there an onload function in JavaScript?

In JavaScript, this event can apply to launch a particular function when the page is fully displayed. It can also be used to verify the type and version of the visitor's browser. We can check what cookies a page uses by using the onload attribute.

How do you call a function on page load?

The first approach for calling a function on the page load is the use an onload event inside the HTML <body> tag. As you know, the HTML body contains the entire content of the web page, and when all HTML body loads on the web browser, it will call the function from the JavaScript.

What is JavaScript onload?

The onload property processes load events after the element has finished loading. This is used with the window element to execute a script after the webpage has completely loaded. The function that is required to be executed is assigned as the handler function to this property.


1 Answers

That appears to be the standard JavaScript DOM Event object. It describes the nature of the event that your function is handling.

UPDATE In response to comment discussion:

Different browsers supply the Event object in different ways:

  • IE never passes it as an argument to the function and instead uses the window.event property.
  • Firefox will pass it as the first argument.
  • Chrome seems to do both.
like image 137
Annabelle Avatar answered Oct 31 '22 03:10

Annabelle