Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do we 'declare' jQuery or we simply use it?

I was asked in an interview that how do you 'declare' jQuery? He did not mean a jQuery variable or a $(func()). In case you find this question weird, please do not penalize me for this as I'm enquiring just cause I was asked. :)

like image 584
Rohan Avatar asked Dec 21 '11 07:12

Rohan


People also ask

Where do I declare jQuery?

Answers. A simple solution for anyone learning jQuery is to always declare your jQuery code inside the document ready function. This is to ensure that all of the html page elements also known as the DOM (Document Object Model) have loaded before any jQuery code is run.

How do I add jQuery to my project?

There are two ways to include jQuery in a project, which is to download a local copy or link to a file via Content Delivery Network (CDN). Note: A Content Delivery Network (CDN) is a system of multiple servers that deliver web content to a user based on geographical location.

Can we write jQuery in JS file?

Dynamic adding jQuery, CSS from js file. When we added onload function to body we can use jQuery to create page from js file.

What is jQuery and why it is used?

jQuery is a lightweight, "write less, do more", JavaScript library. The purpose of jQuery is to make it much easier to use JavaScript on your website. jQuery takes a lot of common tasks that require many lines of JavaScript code to accomplish, and wraps them into methods that you can call with a single line of code.


1 Answers

You don't "declare" jQuery, you just include the file within a script tag:

<script src="/locationof/jQuery.js"></script>

If you look in the jQuery source it appends itself to window.$ and window.jQuery when it runs as the source code is in a self-executing anonymous function.

(function( window, undefined ) {
   // rest of source here

   // Expose jQuery to the global object
   window.jQuery = window.$ = jQuery;
})(window);
like image 122
isNaN1247 Avatar answered Oct 18 '22 11:10

isNaN1247