I don't know the specifics of angular, but there is no reason to not insert any initial data into the page directly. In Fact backbone suggests doing exactly that. All you would have to do is put in your template something like this
<script>
var initial_data = {json with your data here};
</script>
Where you inject the json from whatever server side framework you are using.
A high-level dissection of the code posted:
<script type="text/javascript">
Html-element to embed a script in it
$("#circle")
$ is the jQuery alias for getting the jQuery object which wraps almost all of the stuff jQuery can do. In this case we simply invoke the jQuery object with syntax similar to that of a constructor. This is shorthand for "pass in a css-selector and return all matched DOM elements wrapped as jQuery elements". So we select the element in the DOM with id=circle
.click(
This is (one of many) jQuery-ways to say when the element(s) - the previous selector could have returned a ton of elements - is clicked run the function passed into me. This function is called a callback-function. Basically subscribing to the onclick event of the DOM element.
function() {
.. the callback passed in...
$("p")
Similar to the $("#circle")-statement. Select all paragraph-elements in the DOM as jQuery elements. (<p></p>
)
.html("You clicked the circle");
For all p-elements previously selected set the inner html of this elements to be "You clicked the circle". Note; You should almost never want to do this for security reasons - use .text() instead.
}
.. close the call back ..
)
.. Honorably mention; You forgot to close the click()-function :) The code as-is will still work though, since javascript is forgiving (For better or for worse, that's another story)
</script>
.. closing the script-tag..
As it has been stated, this is a really broad question even though it does not look like it. For all the above paragraphs you could probably write a page of what it really does under the hood, but that's the gist of it.
For your question "Does jQuery convert the resulting code to JavaScript?". Not really. jQuery is implemented in javascript, so you can simply think of it as a wrapper library. jQuery serves two main purposes: 1) Providing an API that works regardless of browser types. So for example, if Safari has some quirks working with the onclick-event but firefox follows the spec, jQuery will take care of this under the hood for you (Not the the Safari/Firefox example is purely made up, none of those should have quirks). And 2) provide QoL-improvements such as CSS-selectors, although those have since been implemented in ES5 or ES6, can't remember which. When jQuery came out you had to call document.getElementById() or a similar DOM-method to select elements.
At the risk of starting a religious war, jQuery has nowadays largely served its purpose. Most of the stuff that made jQuery popular is now implemented in recent ECMAScript-versions and most browsers are now updated to at least include the basic stuff. Personally, I only use it because it's a familiar API, as I used to work extensively with it, but it's not "as required" to use it as it once was.
Hope it helps :)
Best Answer
As you know, it's all about performance!
If you are work on a large project, which covers modern UI concepts, maybe you will have bunch of different JavaScript libraries hosted on your server. As you may know, browsers are limited to maximum of few (see 'connections per hostname' column) HTTP requests to specific host.
Let's say you have a site that needs to load a lot JavaScript libraries, and if they are minified, they are between 20-50KB each.
So, simply math: 10 libraries * 40KB = 400KB!
And it's very important to keep in mind that:
So I think in this case using of CDN (not only for jQuery, and whenever is possible) make sense because:
Increase parallel downloading overcoming the limitation of browsers.
Since CDN is used by a lot of sites nowаdays, there is a really big chance to have already cached version of the resource.
And this point is valid both for your questions:
But there is something very important, which is ignored by many developers. They are using CDN to get latest version of a library, that is completely wrong! When new version is released, although in most cases, new functionality is added, there are cases when existing functionality can be removed (ex. function becomes deprecated) or modified!
So, it's a really good practice always to set a specific version of needed resource when you are using CDN.