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. :)
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.
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.
Dynamic adding jQuery, CSS from js file. When we added onload function to body we can use jQuery to create page from js file.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With