Skip to main content

Bug Tracker

Side navigation

#14802 closed bug (fixed)

Opened February 15, 2014 03:41PM UTC

Closed March 05, 2014 02:50AM UTC

User defined onready function on the window is triggered when jQuery is present on the page

Reported by: harshabn808@yahoo.co.in Owned by: dmethvin
Priority: high Milestone: 1.11.1/2.1.1
Component: core Version: 1.11.0-rc1
Keywords: Cc:
Blocked by: Blocking:
Description

http://www.quora.com/Why-does-jquery-trigger-window-onready

If I define:

function onready(){ console.log("logged"); }

in the global scope, nothing happens.

But if I include jquery, this function will be called.


This is what is happening -

[code]if ( document.addEventListener || event.type === "load" || document.readyState === "complete" ) {

detach();

jQuery.ready();

}[/code]

This snippet of code is called when document's content is loaded (DOMContentLoaded or onreadystatechange). "jQuery.ready()" when called, is triggering a ready event on the document (this is to handle $(document).ready). But jQuery goes further ahead, bubbles up the event to window and checks for the presence of any events of the same type are present on the ancestors. In this case, you do have defined a onready function on the window. So, it calls the function. This is totally wrong and shouldn't happen.

Attachments (0)
Change History (3)

Changed February 15, 2014 04:37PM UTC by dmethvin comment:1

component: unfiledcore
milestone: None1.11.1/2.1.1
priority: undecidedhigh
status: newopen

Agreed, we can just do a .triggerHandler() instead, which won't bubble and won't fire any associated method.

Changed March 04, 2014 03:06AM UTC by dmethvin comment:2

owner: → dmethvin
status: openassigned

Changed March 05, 2014 02:50AM UTC by Dave Methvin comment:3

resolution: → fixed
status: assignedclosed

Core: Do not run window.onready when ready

Fixes #14802

Changeset: 2df1aad6a1c9376c2a477eba26ee992113ed1c23