Saturday, December 7, 2013

Conflict in multiple jQuery files on same page

You may also like to see:

Many libraries use $ character as library main instance variable or function like jQuery. jQuery has created a shorter variable name $ so you can use it quicker instead of using jQuery. In other words using jQuery keyword or $ is same. Problem occurs when using multiple libraries using $ as there variable. For example you are using mootools and jQuery on the same page. Now if you try to use $ to call a function, it will throw an exception as there are two different libraries using this keyword as variable causing conflict.

jQuery allows you to free $ keyword using noConflict() method. So if you called this function now $ is no more associated to jQuery so you can use it for other library like mootools and to use jQuery code, you need to use jQuery keyword. Calling noConflict() with $ or jQuery only releases $ keyword. so jQuery.noConflict() and $.noConflict() are same.





Now if you have already written, code with $ keyword for jQuery or you still want to use $ keyword for jQuery library too, you can do a trick. You can pass $ as alias for jQuery in ready block. It is recommended that to use jQuery code always using this technique as in sometime later in application you may need you use any other library which cause conflict.





or you can initilaize anonymous function passing it object of jQuery as parameter and receive this parameter as $ keyword and use jQuery code with $ within that anonymous function scope.

jQuery.noConflict();
(function( $ ) { //receive parameter as $ alias

 //Now you can use $ for accessing jQuery from here
 $(function() {

   // Use jQuery code with $ keyword
   $( "div p" ).show();
 });

})(jQuery);// call anonymous function and pass jQuery instance variable

// Other code using $ as an alias to the other library
$( "content" ).style.display = "none";


Insead of $ you can also use any other character or word as jQuery instance variable by assigning it using noConflict() function.

var jq = jQuery.noConflict();

//Now you can access jQuery code using jq keyword
jq( "div p" ).show();


What if you are a using multiple jQuery version on same page? jQuery and $ keywords associated with which version?

Lets assume you are using a plugin with a jQuery version and at some time later jQuery updated its version which is required for another plugins. Both plugins of jQuery required different versions of jQuery because they do not support other versions.

So what now you have to do?


Here you may need to free jQuery keyword also because now jQuery keyword will also conflict so you need to free jQuery keyword. You can achieve this by passing true as parameter to noConflict() this will free both keyword from current libraries. You must need to initialize new instance variable for further use.

So now if you need to use multiple version of jQuery for different plugins, you may initialize different variable associated with different version of jQuery by passing true parameter to noConflict()





//you must load second library later after initializing
//first instance of version and freeup jQuery keyword
//else jQuery keyword will
//cause conflict it you uplaoded both files.





//so now here $ and jQuery both cannot be used

//using $jQuery1_10 will call version 1.10.0 library code
$jQuery1_10( "div p" ).show();

//using $jQuery1_9 will call version 1.9.0 library code
$jQuery1_9( "div p" ).show();


Here is Demo

If you want to add something to this post please comment below.

You may also like to see:

Sunday, November 24, 2013

List of freely available Programming Books

You may also like to see:

Here I listed freely available Programming languages books. The list contains almost all the programming languages and technologies.

    Graphics Programming

    Graphics User Interfaces

    Language Agnostic

    Algorithms & Datastructures

    Theoretical Computer Science

    Operating systems

    Database

    Networking

    Compiler Design

    Programming Paradigms

    Parallel Programming

    Software Architecture

    Open Source Ecosystem

    Datamining

    Machine Learning

    Mathematics

    Cellular Automata

    Misc

    Web Performance

    MOOC

    Professional Development

    Security

    Ada

    Agda

    Android

    APL

    Autotools

    ASP.NET MVC

    Assembly Language

    Non-X86

    Bash

    C

    C++

    Clojure

    COBOL

    CoffeeScript

    ColdFusion

    Coq

    D

    Dart

    DTrace

    DB2

    Delphi / Pascal

    Elasticsearch

    Emacs

    Erlang

    Flex

    F Sharp

    Force.com

    Forth

    Git

    Go

    Grails

    Hadoop

    Haskell

    HTML / CSS

    Icon

    IDL

    iOS

    J

    Java

    Wicket

    JavaScript

    Angular.js

    Backbone.js

    D3.js

    Dojo

    jQuery

    Knockout.js

    Node.js


    LaTeX

    See also TeX

    Linux

    Lisp

    Lua

    Mathematica

    MATLAB

    Maven

    Mercurial

    MySQL

    .NET (C# / VB / Nemerle / Visual Studio)

    NoSQL

    Oberon

    Objective-C

    OCaml

    Octave

    OpenGL ES

    OpenSCAD

    Oracle Server

    Oracle PL/SQL

    Parrot / Perl 6

    Perl

    PHP

    PowerShell

    Processing

    Prolog

    PostgreSQL

    Python

    Django

    Flask

    R

    Racket

    Ruby

    RSpec

    Sinatra

    Ruby on Rails

    Rust

    Sage

    Scala

    Scheme

    Scilab

    Scratch

    Sed

    Silverlight

    Smalltalk

    Subversion

    SQL (implementation agnostic)

    SQL Server

    Teradata

    TeX

    See also LaTeX

    Theory

    TypeScript

    Unix

    Vim

    Websphere

    Windows Phone

    Windows 8


    Reference : The list is taken from

    You may also like to see:
    Life insurance policy, bank loans, software, microsoft, facebook,mortgage,policy,