jQuery Migrate
repository·main·Indexed 24 days ago
https://github.com/jquery/jquery-migrateA compatibility plugin that restores removed jQuery APIs and provides console warnings for deprecated features to facilitate smoother upgrades to newer jQuery versions. It includes development and production builds, a Migrate Plugin API for controlling behavior and inspecting migration status, and specific guidance for migrating older code to jQuery 4.x.
What's inside jquery-migrate
- jQuery Migrate is a plugin designed to ease the process of upgrading jQuery. It works by restoring APIs that were removed in newer versions of jQuery and providing warnings in the browser console (when using the development version) when deprecated or removed APIs are used. This allows developers to identify and fix compatibility issues before eventually removing the plugin once the codebase is fully updated.
Fix [data-null-proto] JQMIGRATE: Data objects no longer inherit from Object.prototype
mainAs of jQuery 4.0.0, data objects returned by
jQuery.data()orjQuery.fn.data()no longer inherit fromObject.prototype. This means properties like__proto__orhasOwnPropertyare not available on the data object itself.Solution: Use
Object.hasOwn()orObject.prototype.hasOwnProperty.call()to check for properties.Example:
- Instead of
jQuery.data( node ).hasOwnProperty( "foo" ) - Use
Object.hasOwn( jQuery.data( node ), "foo" )(modern) - Or
Object.prototype.hasOwnProperty.call( jQuery.data( node ), "foo" )(for IE 11 support).
- Instead of
Handle boolean attribute changes in jQuery 4.0
mainPrior to jQuery 4.0,
.attr( name, value )with a non-false/non-nullvalue would set the attribute to its name (e.g.,checked). jQuery 4.0 removes this behavior. Additionally,.attr( name )no longer returns the name lowercased for boolean attributes.Solutions:
- Setting attributes: Always set boolean attributes to their names explicitly using
.attr( name, name ), native.setAttribute( name, name ), or HTML<input checked="checked">. - Getting attributes: Avoid using
.attr( name )to check boolean attributes; use.prop( name )instead.
- Setting attributes: Always set boolean attributes to their names explicitly using
Choose between Development and Production versions of jQuery Migrate
mainjQuery Migrate provides two versions to suit different environments:
- Development (uncompressed) version: Generates console warning messages whenever compatibility issues are detected. This is intended for developers to identify and fix code during migration.
- Production (compressed) version: Does not generate warnings. Use this version in production environments to maintain compatibility without cluttering the console or impacting performance with unnecessary logging.
Note: Warnings are printed as
console.warnfor removed APIs (which must be fixed for the code to work without the plugin) and asconsole.infofor deprecated APIs (which are still supported but should be updated).Install and use jQuery Migrate
mainTo use jQuery Migrate, load the plugin script tag after the jQuery script tag in your web page.
<script src="https://code.jquery.com/jquery-4.0.0.js"></script> <script src="https://code.jquery.com/jquery-migrate-4.0.2.js"></script>Choose between Development and Production versions
mainSelect the appropriate build based on your environment:
- Development version: Includes debugging features and generates console warnings for deprecated/removed APIs. Use this for development and debugging.
- Production version: Minified and does not generate console warnings. It only logs a message upon loading or if it detects an incompatible jQuery version. Use this for live sites.
Build Type Debugging Minified Latest Release Development ✓ No jquery-migrate-4.0.2.jsProduction No ✓ jquery-migrate-4.0.2.min.jsDebug with jQuery Migrate warnings
mainThe development version of the plugin displays warnings in the browser console. All warnings start with the string
JQMIGRATE.You can also programmatically inspect the warnings generated by your code by checking the
jQuery.migrateMessagesarray.Replace jQuery event shorthands with .on() and .trigger()
mainShorthand methods like
.click(fn)are deprecated. You should use the more explicit.on()and.trigger()methods.This applies to many event types, including:
blur,focus,focusin,focusout,resize,scroll,dblclick,mousedown,mouseup,mousemove,mouseover,mouseout,mouseenter,mouseleave,change,select,submit,keydown,keypress,keyup, andcontextmenu.Solution:
- Instead of
.click(fn), use.on("click", fn). - Instead of
.click(), use.trigger("click").
- Instead of
Troubleshoot JQMIGRATE: jQuery 4.x REQUIRED
mainThis error occurs when the page either has no jQuery installed, or is using a version of jQuery that is incompatible with this specific version of the Migrate plugin (specifically, versions older than 4.0.0 or 5.0.0 and newer).
Solution: Ensure you are using a supported version of jQuery as specified in the project README.
Address deprecated jQuery.holdReady() performance issues
mainThe
jQuery.holdReady()method is deprecated because it can negatively impact global page performance by preventing all code on the page from initializing for extended periods.Solution: Rewrite the page logic so that it does not require delaying all jQuery ready handlers. A common approach is to late-load only the specific code that requires a delay when it is safe to run.
Note: jQuery Migrate does not provide a polyfill for
holdReady(). If the underlying jQuery version no longer contains this method, your code will fail.Replace deprecated event binding methods (.bind, .unbind, .delegate, .undelegate)
mainThe methods
jQuery.fn.bind(),jQuery.fn.unbind(),jQuery.fn.delegate(), andjQuery.fn.undelegate()are deprecated in favor of.on()and.off(). While they still work in jQuery 4.x, they may be removed in future major versions.Solution: Use
.on()and.off()for both direct and delegated event binding.For simple direct bindings, you can often rename the methods directly because the argument orders are identical:
- Replace
.bind()with.on() - Replace
.unbind()with.off()
- Replace
Fix [attr-false] JQMIGRATE: Setting non-ARIA non-boolean attributes to false
mainIn jQuery 4.x, calling
.attr( name, false )on a non-ARIA, non-boolean attribute will remove the attribute entirely. In previous versions, it would set the attribute value to the string"false".Solution: If you intend to set the attribute value to the string
"false", wrap it in quotes:.attr( name, "false" ).