Changing all inefficient selectors of $('body') to $(document.body), which is about 80% faster (and even more on mobile, see https://jsperf.com/jquery-body-vs-document-body-selector/2 ).

This commit is contained in:
Danny van Kooten 2015-04-03 14:21:47 +02:00
parent 5b1411c771
commit 88f2e690bb
18 changed files with 67 additions and 67 deletions

View File

@ -14,7 +14,7 @@ jQuery( function ( $ ) {
$( '.js_field-country' ).select2().change( this.change_country );
$( '.js_field-country' ).trigger( 'change', [ true ] );
$( 'body' ).on( 'change', 'select.js_field-state', this.change_state );
$(document.body).on( 'change', 'select.js_field-state', this.change_state );
$( '#woocommerce-order-actions input, #woocommerce-order-actions a' ).click(function() {
window.onbeforeunload = '';
});
@ -70,7 +70,7 @@ jQuery( function ( $ ) {
$state.replaceWith( '<input type="text" class="js_field-state" name="' + input_name + '" id="' + input_id + '" value="' + value + '" placeholder="' + placeholder + '" />' );
}
$( 'body' ).trigger( 'contry-change.woocommerce', [country, $( this ).closest( 'div' )] );
$(document.body).trigger( 'contry-change.woocommerce', [country, $( this ).closest( 'div' )] );
},
change_state: function() {
@ -992,7 +992,7 @@ jQuery( function ( $ ) {
init: function( e, target ) {
if ( '#wc-modal-add-products' === target ) {
$( 'body' ).trigger( 'wc-enhanced-select-init' );
$(document.body).trigger( 'wc-enhanced-select-init' );
}
},
@ -1213,7 +1213,7 @@ jQuery( function ( $ ) {
window.alert( woocommerce_admin_meta_boxes.i18n_download_permission_fail );
}
$('body').trigger( 'wc-init-datepickers' );
$(document.body).trigger( 'wc-init-datepickers' );
$( '#grant_access_id' ).val( '' ).change();
$( '.order_download_permissions' ).unblock();
});

View File

@ -107,12 +107,12 @@ jQuery( function( $ ){
$( 'ul.wc-tabs li:visible' ).eq(0).find( 'a' ).click();
$( 'body' ).trigger( 'woocommerce-product-type-change', select_val, $( this ) );
$(document.body).trigger( 'woocommerce-product-type-change', select_val, $( this ) );
}).change();
$( 'body' ).on( 'woocommerce-product-type-change', function( e, select_val ) {
if ( 'variable' !== select_val && 0 < $( '#variable_product_options input[name^=variable_sku]' ).length && $( 'body' ).triggerHandler( 'woocommerce-display-product-type-alert', select_val ) !== false ) {
$(document.body).on( 'woocommerce-product-type-change', function( e, select_val ) {
if ( 'variable' !== select_val && 0 < $( '#variable_product_options input[name^=variable_sku]' ).length && $(document.body).triggerHandler( 'woocommerce-display-product-type-alert', select_val ) !== false ) {
window.alert( woocommerce_admin_meta_boxes.i18n_product_type_alert );
}
});
@ -293,11 +293,11 @@ jQuery( function( $ ){
$wrapper.find( '.enable_variation' ).hide();
}
$('body').trigger( 'wc-enhanced-select-init' );
$(document.body).trigger( 'wc-enhanced-select-init' );
attribute_row_indexes();
$wrapper.unblock();
$('body').trigger( 'woocommerce_added_attribute' );
$(document.body).trigger( 'woocommerce_added_attribute' );
});
if ( attribute ) {

View File

@ -48,7 +48,7 @@ jQuery( function ( $ ) {
});
$('ul.wc-tabs li:visible').eq(0).find('a').click();
$('body').on( 'wc-init-datepickers', function() {
$(document.body).on( 'wc-init-datepickers', function() {
$( ".date-picker-field, .date-picker" ).datepicker({
dateFormat: "yy-mm-dd",
numberOfMonths: 1,
@ -56,7 +56,7 @@ jQuery( function ( $ ) {
});
});
$('body').trigger( 'wc-init-datepickers' );
$(document.body).trigger( 'wc-init-datepickers' );
// META BOXES - Open/close
$('.wc-metaboxes-wrapper').on('click', '.wc-metabox h3', function(event){

View File

@ -61,7 +61,7 @@
render: function() {
this.$el.attr( 'tabindex' , '0' ).append( $( this._target ).html() );
$( 'body' ).css({
$(document.body).css({
'overflow': 'hidden'
}).append( this.$el );
@ -89,21 +89,21 @@
'margin-top': '-' + ( $( '.wc-backbone-modal-content' ).height() / 2 ) + 'px'
});
$( 'body' ).trigger( 'wc_backbone_modal_loaded', this._target );
$(document.body).trigger( 'wc_backbone_modal_loaded', this._target );
},
closeButton: function( e ) {
e.preventDefault();
$( 'body' ).trigger( 'wc_backbone_modal_before_remove', this._target );
$(document.body).trigger( 'wc_backbone_modal_before_remove', this._target );
this.undelegateEvents();
$( document ).off( 'focusin' );
$( 'body' ).css({
$(document.body).css({
'overflow': 'auto'
});
this.remove();
$( 'body' ).trigger( 'wc_backbone_modal_removed', this._target );
$(document.body).trigger( 'wc_backbone_modal_removed', this._target );
},
addButton: function( e ) {
$( 'body' ).trigger( 'wc_backbone_modal_response', [ this._target, this.getFormData() ] );
$(document.body).trigger( 'wc_backbone_modal_response', [ this._target, this.getFormData() ] );
this.closeButton( e );
},
getFormData: function() {

View File

@ -14,7 +14,7 @@ jQuery( function ( $ ) {
$( '.js_field-country' ).select2().change( this.change_country );
$( '.js_field-country' ).trigger( 'change', [ true ] );
$( 'body' ).on( 'change', 'select.js_field-state', this.change_state );
$(document.body).on( 'change', 'select.js_field-state', this.change_state );
},
change_country: function( e, stickValue ) {
@ -63,7 +63,7 @@ jQuery( function ( $ ) {
$state.replaceWith( '<input type="text" class="js_field-state" name="' + input_name + '" id="' + input_id + '" value="' + value + '" placeholder="' + placeholder + '" />' );
}
$( 'body' ).trigger( 'contry-change.woocommerce', [country, $( this ).closest( 'div' )] );
$(document.body).trigger( 'contry-change.woocommerce', [country, $( this ).closest( 'div' )] );
},
change_state: function() {

View File

@ -6,12 +6,12 @@
jQuery( function ( $ ) {
// Price input validation
$('body').on( 'blur', '.wc_input_decimal[type=text], .wc_input_price[type=text], .wc_input_country_iso[type=text]', function() {
$(document.body).on( 'blur', '.wc_input_decimal[type=text], .wc_input_price[type=text], .wc_input_country_iso[type=text]', function() {
$('.wc_error_tip').fadeOut('100', function(){ $(this).remove(); } );
return this;
});
$('body').on('keyup change', '.wc_input_price[type=text]', function(){
$(document.body).on('keyup change', '.wc_input_price[type=text]', function(){
var value = $(this).val();
var regex = new RegExp( "[^\-0-9\%.\\" + woocommerce_admin.mon_decimal_point + "]+", "gi" );
var newvalue = value.replace( regex, '' );
@ -30,7 +30,7 @@ jQuery( function ( $ ) {
return this;
});
$('body').on('keyup change', '.wc_input_decimal[type=text]', function(){
$(document.body).on('keyup change', '.wc_input_decimal[type=text]', function(){
var value = $(this).val();
var regex = new RegExp( "[^\-0-9\%.\\" + woocommerce_admin.decimal_point + "]+", "gi" );
var newvalue = value.replace( regex, '' );
@ -49,7 +49,7 @@ jQuery( function ( $ ) {
return this;
});
$('body').on( 'keyup', '#_sale_price.wc_input_price[type=text], .wc_input_price[name^=variable_sale_price]', function(){
$(document.body).on( 'keyup', '#_sale_price.wc_input_price[type=text], .wc_input_price[name^=variable_sale_price]', function(){
var sale_price_field = $(this);
if( sale_price_field.attr('name').indexOf('variable') !== -1 ) {
@ -75,7 +75,7 @@ jQuery( function ( $ ) {
}
return this;
});
$('body').on( 'change', '#_sale_price.wc_input_price[type=text], .wc_input_price[name^=variable_sale_price]', function(){
$(document.body).on( 'change', '#_sale_price.wc_input_price[type=text], .wc_input_price[name^=variable_sale_price]', function(){
var sale_price_field = $(this);
if( sale_price_field.attr('name').indexOf('variable') !== -1 ) {
@ -95,7 +95,7 @@ jQuery( function ( $ ) {
return this;
});
$('body').on('keyup change', '.wc_input_country_iso[type=text]', function(){
$(document.body).on('keyup change', '.wc_input_country_iso[type=text]', function(){
var value = $(this).val();
var regex = new RegExp( '^([A-Z])?([A-Z])$' );
@ -222,7 +222,7 @@ jQuery( function ( $ ) {
}).change();
// Show order items on orders page
$('body').on( 'click', '.show_order_items', function() {
$(document.body).on( 'click', '.show_order_items', function() {
$(this).closest('td').find('table').toggle();
return false;
});

View File

@ -1146,7 +1146,7 @@
'style': style_block
});
div.text(this.search_field.val());
$('body').append(div);
$(document.body).append(div);
w = div.width() + 25;
div.remove();
f_width = this.container.outerWidth();

View File

@ -29,6 +29,6 @@ jQuery( function( $ ) {
$( '#add_payment_method' ).block({ message: null, overlayCSS: { background: '#fff', opacity: 0.6 } });
});
$( 'body' ).trigger( 'init_add_payment_method' );
$(document.body).trigger( 'init_add_payment_method' );
});

View File

@ -25,7 +25,7 @@ jQuery( function( $ ) {
});
// Trigger event
$( 'body' ).trigger( 'adding_to_cart', [ $thisbutton, data ] );
$(document.body).trigger( 'adding_to_cart', [ $thisbutton, data ] );
// Ajax action
$.post( wc_add_to_cart_params.wc_ajax_url + 'add_to_cart', data, function( response ) {
@ -94,7 +94,7 @@ jQuery( function( $ ) {
$( '.shop_table.cart' ).stop( true ).css( 'opacity', '1' ).unblock();
$( 'body' ).trigger( 'cart_page_refreshed' );
$(document.body).trigger( 'cart_page_refreshed' );
});
$( '.cart_totals' ).load( this_page + ' .cart_totals:eq(0) > *', function() {
@ -102,7 +102,7 @@ jQuery( function( $ ) {
});
// Trigger event so themes can refresh other areas
$( 'body' ).trigger( 'added_to_cart', [ fragments, cart_hash, $thisbutton ] );
$(document.body).trigger( 'added_to_cart', [ fragments, cart_hash, $thisbutton ] );
}
});

View File

@ -30,14 +30,14 @@ jQuery( function( $ ) {
sessionStorage.setItem( 'wc_cart_hash', data.cart_hash );
}
$( 'body' ).trigger( 'wc_fragments_refreshed' );
$(document.body).trigger( 'wc_fragments_refreshed' );
}
}
};
if ( $supports_html5_storage ) {
$( 'body' ).bind( 'added_to_cart', function( event, fragments, cart_hash ) {
$(document.body).bind( 'added_to_cart', function( event, fragments, cart_hash ) {
sessionStorage.setItem( wc_cart_fragments_params.fragment_name, JSON.stringify( fragments ) );
sessionStorage.setItem( 'wc_cart_hash', cart_hash );
});
@ -61,7 +61,7 @@ jQuery( function( $ ) {
$( key ).replaceWith(value);
});
$( 'body' ).trigger( 'wc_fragments_loaded' );
$(document.body).trigger( 'wc_fragments_loaded' );
} else {
throw 'No fragment';
@ -82,7 +82,7 @@ jQuery( function( $ ) {
$( '.hide_cart_widget_if_empty' ).closest( '.widget_shopping_cart' ).hide();
}
$( 'body' ).bind( 'adding_to_cart', function() {
$(document.body).bind( 'adding_to_cart', function() {
$( '.hide_cart_widget_if_empty' ).closest( '.widget_shopping_cart' ).show();
} );

View File

@ -34,7 +34,7 @@ jQuery( function( $ ) {
$.post( wc_cart_params.wc_ajax_url + 'update_shipping_method', data, function( response ) {
$( 'div.cart_totals' ).replaceWith( response );
$( 'body' ).trigger( 'updated_shipping_method' );
$(document.body).trigger( 'updated_shipping_method' );
});
});

View File

@ -14,9 +14,9 @@ jQuery( function( $ ) {
$order_review: $( '#order_review' ),
$checkout_form: $( 'form.checkout' ),
init: function() {
$( 'body' ).bind( 'update_checkout', this.reset_update_checkout_timer );
$( 'body' ).bind( 'update_checkout', this.update_checkout );
$( 'body' ).bind( 'init_checkout', this.init_checkout );
$( document.body ).bind( 'update_checkout', this.reset_update_checkout_timer );
$( document.body ).bind( 'update_checkout', this.update_checkout );
$( document.body ).bind( 'init_checkout', this.init_checkout );
// Payment methods
this.$order_review.on( 'click', 'input[name=payment_method]', this.payment_method_selected );
@ -42,7 +42,7 @@ jQuery( function( $ ) {
// Update on page load
if ( wc_checkout_params.is_checkout === '1' ) {
$( 'body' ).trigger( 'init_checkout' );
$(document.body).trigger( 'init_checkout' );
}
if ( wc_checkout_params.option_guest_checkout === 'yes' ) {
$( 'input#createaccount' ).change( this.toggle_create_account ).change();
@ -57,7 +57,7 @@ jQuery( function( $ ) {
},
init_checkout: function( e ) {
$( '#billing_country, #shipping_country, .country_to_state' ).change();
$( 'body' ).trigger( 'update_checkout' );
$(document.body).trigger( 'update_checkout' );
},
maybe_input_changed: function( e ) {
if ( wc_checkout_form.dirtyInput ) {
@ -82,13 +82,13 @@ jQuery( function( $ ) {
trigger_update_checkout: function( e ) {
wc_checkout_form.reset_update_checkout_timer();
wc_checkout_form.dirtyInput = false;
$( 'body' ).trigger( 'update_checkout' );
$(document.body).trigger( 'update_checkout' );
},
maybe_update_checkout: function() {
var update_totals = true;
if ( $( wc_checkout_form.dirtyInput ).size() ) {
$required_inputs = $( wc_checkout_form.dirtyInput ).closest( 'div' ).find( '.address-field.validate-required' );
var $required_inputs = $( wc_checkout_form.dirtyInput ).closest( 'div' ).find( '.address-field.validate-required' );
if ( $required_inputs.size() ) {
$required_inputs.each( function( e ) {
@ -287,7 +287,7 @@ jQuery( function( $ ) {
$( '.woocommerce-checkout' ).find( 'input[name=payment_method]:checked' ).eq(0).trigger( 'click' );
// Fire updated_checkout e
$( 'body' ).trigger( 'updated_checkout' );
$(document.body).trigger( 'updated_checkout' );
}
});
@ -378,10 +378,10 @@ jQuery( function( $ ) {
// Trigger update in case we need a fresh nonce
if ( result.refresh === 'true' ) {
$( 'body' ).trigger( 'update_checkout' );
$(document.body).trigger( 'update_checkout' );
}
$( 'body' ).trigger( 'checkout_error' );
$(document.body).trigger( 'checkout_error' );
}
},
dataType: 'html'
@ -395,8 +395,8 @@ jQuery( function( $ ) {
var wc_checkout_coupons = {
init: function() {
$( 'body' ).on( 'click', 'a.showcoupon', this.show_coupon_form );
$( 'body' ).on( 'click', '.woocommerce-remove-coupon', this.remove_coupon );
$( document.body ).on( 'click', 'a.showcoupon', this.show_coupon_form );
$( document.body ).on( 'click', '.woocommerce-remove-coupon', this.remove_coupon );
$( 'form.checkout_coupon' ).hide().submit( this.submit );
},
show_coupon_form: function( e ) {
@ -435,7 +435,7 @@ jQuery( function( $ ) {
$form.before( code );
$form.slideUp();
$( 'body' ).trigger( 'update_checkout' );
$(document.body).trigger( 'update_checkout' );
}
},
dataType: 'html'
@ -473,7 +473,7 @@ jQuery( function( $ ) {
if ( code ) {
$( 'form.woocommerce-checkout' ).before( code );
$( 'body' ).trigger( 'update_checkout' );
$(document.body).trigger( 'update_checkout' );
// remove coupon code from coupon field
$( 'form.checkout_coupon' ).find( 'input[name="coupon_code"]' ).val( '' );
@ -491,7 +491,7 @@ jQuery( function( $ ) {
var wc_checkout_login_form = {
init: function() {
$( 'body' ).on( 'click', 'a.showlogin', this.show_login_form );
$(document.body).on( 'click', 'a.showlogin', this.show_login_form );
},
show_login_form: function( e ) {
$( 'form.login' ).slideToggle();

View File

@ -74,7 +74,7 @@ jQuery( function( $ ) {
wc_country_select_select2();
$( 'body' ).bind( 'country_to_state_changed', function() {
$(document.body).bind( 'country_to_state_changed', function() {
wc_country_select_select2();
});
}
@ -83,7 +83,7 @@ jQuery( function( $ ) {
var states_json = wc_country_select_params.countries.replace( /&quot;/g, '"' ),
states = $.parseJSON( states_json );
$( 'body' ).on( 'change', 'select.country_to_state, input.country_to_state', function() {
$(document.body).on( 'change', 'select.country_to_state, input.country_to_state', function() {
var country = $( this ).val(),
$statebox = $( this ).closest( 'div' ).find( '#billing_state, #shipping_state, #calc_shipping_state' ),
@ -99,7 +99,7 @@ jQuery( function( $ ) {
$statebox.parent().hide().find( '.select2-container' ).remove();
$statebox.replaceWith( '<input type="hidden" class="hidden" name="' + input_name + '" id="' + input_id + '" value="" placeholder="' + placeholder + '" />' );
$( 'body' ).trigger( 'country_to_state_changed', [country, $( this ).closest( 'div' )] );
$(document.body).trigger( 'country_to_state_changed', [country, $( this ).closest( 'div' )] );
} else {
@ -123,7 +123,7 @@ jQuery( function( $ ) {
$statebox.html( '<option value="">' + wc_country_select_params.i18n_select_state_text + '</option>' + options );
$statebox.val( value ).change();
$( 'body' ).trigger( 'country_to_state_changed', [country, $( this ).closest( 'div' )] );
$(document.body).trigger( 'country_to_state_changed', [country, $( this ).closest( 'div' )] );
}
} else {
@ -132,19 +132,19 @@ jQuery( function( $ ) {
$parent.show().find( '.select2-container' ).remove();
$statebox.replaceWith( '<input type="text" class="input-text" name="' + input_name + '" id="' + input_id + '" placeholder="' + placeholder + '" />' );
$( 'body' ).trigger( 'country_to_state_changed', [country, $( this ).closest( 'div' )] );
$(document.body).trigger( 'country_to_state_changed', [country, $( this ).closest( 'div' )] );
} else if ( $statebox.is( '.hidden' ) ) {
$parent.show().find( '.select2-container' ).remove();
$statebox.replaceWith( '<input type="text" class="input-text" name="' + input_name + '" id="' + input_id + '" placeholder="' + placeholder + '" />' );
$( 'body' ).trigger( 'country_to_state_changed', [country, $( this ).closest( 'div' )] );
$(document.body).trigger( 'country_to_state_changed', [country, $( this ).closest( 'div' )] );
}
}
$( 'body' ).trigger( 'country_to_state_changing', [country, $( this ).closest( 'div' )] );
$(document.body).trigger( 'country_to_state_changing', [country, $( this ).closest( 'div' )] );
});

View File

@ -19,7 +19,7 @@ jQuery( function( $ ) {
if ( woocommerce_price_slider_params.min_price ) current_min_price = parseInt( woocommerce_price_slider_params.min_price, 10 );
if ( woocommerce_price_slider_params.max_price ) current_max_price = parseInt( woocommerce_price_slider_params.max_price, 10 );
$( 'body' ).bind( 'price_slider_create price_slider_slide', function( event, min, max ) {
$(document.body).bind( 'price_slider_create price_slider_slide', function( event, min, max ) {
if ( woocommerce_price_slider_params.currency_pos === 'left' ) {
$( '.price_slider_amount span.from' ).html( woocommerce_price_slider_params.currency_symbol + min );
@ -42,7 +42,7 @@ jQuery( function( $ ) {
}
$( 'body' ).trigger( 'price_slider_updated', min, max );
$(document.body).trigger( 'price_slider_updated', min, max );
});
$( '.price_slider' ).slider({
@ -56,18 +56,18 @@ jQuery( function( $ ) {
$( '.price_slider_amount #min_price' ).val( current_min_price );
$( '.price_slider_amount #max_price' ).val( current_max_price );
$( 'body' ).trigger( 'price_slider_create', [ current_min_price, current_max_price ] );
$(document.body).trigger( 'price_slider_create', [ current_min_price, current_max_price ] );
},
slide: function( event, ui ) {
$( 'input#min_price' ).val( ui.values[0] );
$( 'input#max_price' ).val( ui.values[1] );
$( 'body' ).trigger( 'price_slider_slide', [ ui.values[0], ui.values[1] ] );
$(document.body).trigger( 'price_slider_slide', [ ui.values[0], ui.values[1] ] );
},
change: function( event, ui ) {
$( 'body' ).trigger( 'price_slider_change', [ ui.values[0], ui.values[1] ] );
$(document.body).trigger( 'price_slider_change', [ ui.values[0], ui.values[1] ] );
},
});

View File

@ -473,7 +473,7 @@
var els;
if (full) // crazy selector to handle odd field errors in ie6/7
els = $('body').children().filter('.blockUI').add('body > .blockUI');
els = $(document.body).children().filter('.blockUI').add('body > .blockUI');
else
els = $el.find('>.blockUI');

View File

@ -751,7 +751,7 @@
settings.markup = settings.markup.replace('{pp_social}','');
$('body').append(settings.markup); // Inject the markup
$(document.body).append(settings.markup); // Inject the markup
$pp_pic_holder = $('.pp_pic_holder') , $ppt = $('.ppt'), $pp_overlay = $('div.pp_overlay'); // Set my global selectors

View File

@ -779,7 +779,7 @@ if ( ! defined( 'ABSPATH' ) ) {
'delay': 0
});
$( 'body' ).on( 'copy', '#copy-for-support', function ( e ) {
$(document.body).on( 'copy', '#copy-for-support', function ( e ) {
e.clipboardData.clearData();
e.clipboardData.setData( 'text/plain', $( '#debug-report textarea' ).val() );
e.preventDefault();

View File

@ -75,7 +75,7 @@
$( function () {
$( 'body' ).on( 'checkout_error', function () {
$(document.body).on( 'checkout_error', function () {
$( '.simplify-token' ).remove();
});