jquery solves the problem of naming conflicts that are caused by referencing multiple JavaScript libraries

Source: Internet
Author: User
Tags jquery library

In some cases, we might use multiple JavaScript libraries in one page, because the $ identifier is used in multiple libraries, so we need a way to resolve the naming conflict, and the jquery Noconflict () method solves the problem well. jquery calls this method to pass the control permission on the $ identifier to other libraries for use.

The reference examples are as follows:

< Script src="Prototype.js"></script><!-- Referenced library 1 --

< Script src="Jquery.js"></script> <!-- referenced library 2 --

< Script >

Jquery.noconflict (); / * JQuery Release the $ the right to control */

</ Script >

< Script src="Myscript.js"></script> <!-- wrote it yourself. JS code, at which time the $ identifier is referenced in the library prototype.js- -

As the code in the table shows, we first referenced the Prototype.js library and then referenced the Jquery.js library, at which time the jquery library had control over the $ identifier. Call the Noconflict () method, at which time jquery releases control over the $ identifier and transfers it to prototype.js.

jquery frees the use of the $ identifier, so all subsequent jquery uses jquery instead, such as $ (document). Ready (function () {}), which needs to be written in jquery (document). Ready (function () {}). Of course, love. Inside the ready function, we also have a way to re-enable jquery to use the $ identifier-using a callback function, but only within the ready function. Method: JQuery (document). Ready (function ($) {///where the identifier} can be used normally ).

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

jquery solves the problem of naming conflicts that are caused by referencing multiple JavaScript libraries

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.