diff --git a/1-js/1-getting-started/1-intro/article.md b/1-js/1-getting-started/1-intro/article.md index 6fe5c080..b06e591e 100644 --- a/1-js/1-getting-started/1-intro/article.md +++ b/1-js/1-getting-started/1-intro/article.md @@ -68,7 +68,7 @@ The examples of such restrictions are: Modern browsers allow it to work with files, but the access is limited and only provided if the user does certain actions, like "dropping" a file into a browser window or selecting it via an `` tag. There are ways to interact with camera/microphone and other devices, but they require an explicit user's permission. So a JavaScript-enabled page may not sneakily enable a web-camera, observe the surroundings and send the information to the [NSA](https://en.wikipedia.org/wiki/National_Security_Agency). -- Different tabs/windows generally do not know about each other. Sometimes they do, for example when one window uses JavaScript to open the other one. Such action is allowed. But even in this case, JavaScript from one page may not access the other if they compe from different sites (from a different domain, protocol or port). +- Different tabs/windows generally do not know about each other. Sometimes they do, for example when one window uses JavaScript to open the other one. But even in this case, JavaScript from one page may not access the other if they compe from different sites (from a different domain, protocol or port). That is called a "Same Origin Policy". To workaround that, *both pages* must contain a special JavaScript code that handles data exchange. diff --git a/1-js/1-getting-started/2-editor/article.md b/1-js/1-getting-started/2-editor/article.md new file mode 100644 index 00000000..5e1124da --- /dev/null +++ b/1-js/1-getting-started/2-editor/article.md @@ -0,0 +1,61 @@ +# Code editors + +The code editor is the place where a programmer spends most his time. + +There are two archetypes: IDE and lightweight editors. Many people feel comfortable choosing one tool of each type. + +[cut] + +## IDE + +The term [IDE](https://en.wikipedia.org/wiki/Integrated_development_environment) (Integrated Development Environment) is a powerful editor with many additional features that usually operates on a "whole project". + +An IDE loads the project (can be many files), and then allows navigate between files, provides autocompletion based on the whole project, does other "project-level" stuff. + +If you haven't considered selecting an IDE, pleae look at the following variants: + +- IntelliJ editors: [WebStorm](http://www.jetbrains.com/webstorm/) for frontend development and [PHPStorm (PHP)](http://www.jetbrains.com/phpstorm/), [IDEA (Java)](http://www.jetbrains.com/idea/), [RubyMine (Ruby)](http://www.jetbrains.com/ruby/) and other if you need additional languages. +- Visual Studio is fine if you're a .NET developer. +- Eclipse-based products, like [Aptana](http://www.aptana.com/) and Zend Studio. +- [Komodo IDE](http://www.activestate.com/komodo-ide) and it's lightweight free version [Komodo Edit](http://www.activestate.com/komodo-edit). +- [Netbeans](http://netbeans.org/) + +All of them with the exception of Visual Studio are cross-platform. + +Most IDEs are paid, but have a trial period. Their cost is usually negligible compared to a qualified developer's salary, so just choose the best one for you. + +## Lightweight editors + +"Lightweight editors" are not as powerful as IDEs, but they're fast, elegant and simple. + +They are mainly used to instantly open and edit a file. + +The main difference between a "lightweight editor" and an "IDE" is that the latter works on a project-level, meaning it has to load a lot of data to start, and the former one opens just the files. That's much faster. + +In practice, "lightweight" editors may have a lot of plugins including directory-level syntax analyzers and autocompleters, so there's no strict border between a lightweight editor and an IDE. + +The following options deserve your attention: + +- [Sublime Text](http://www.sublimetext.com) (cross-platform, shareware). +- [Atom](https://atom.io/) (cross-platform, free). +- [Notepad++](http://sourceforge.net/projects/notepad-plus/) (Windows, free). +- Vim, Emacs are cool. If you know how to use them. + +## My favorites + +I believe one should have both an IDE for projects and a lightweight editor for quick and easy file editing. + +I'm using: + +- [WebStorm](http://www.jetbrains.com/webstorm/) for JS, and if there is one more language in the project, then I switch to other Jetbrains editors like [PHPStorm](http://www.jetbrains.com/phpstorm/) (PHP), [IDEA](http://www.jetbrains.com/idea/) (Java), [RubyMine](http://www.jetbrains.com/ruby/) (Ruby). There are editors for other languages too, but I didn't use them. +- As a lightweight editor -- [Sublime Text](http://www.sublimetext.com). + +If you don't know what to choose -- you can consider these ones. + +## Let's not argue + +The editors in the lists above are those that me or my friends -- good developers use for a long time and are happy with. + +There are other great editors in our big world, please choose the one you like the most. + +The choice of an editor, like any other tool, is individual and depends on your projects, habbits, personal preferences. diff --git a/1-js/1-getting-started/3-devtools/article.md b/1-js/1-getting-started/3-devtools/article.md new file mode 100644 index 00000000..9e0a87f5 --- /dev/null +++ b/1-js/1-getting-started/3-devtools/article.md @@ -0,0 +1,66 @@ +# Developer console + +And the one more thing before we get down to coding. + +A code is error-prone. You are quite likely to have errors... Oh what I'm talking? You are *absolutely* going to make errors, at least if you're a human, not a [robot](https://en.wikipedia.org/wiki/Bender_(Futurama)). + +But in the browser, a user doesn't see the errors by default. So, if something goes wrong in the script, we won't see what's broken and can't fix it. + +To see errors and get a lot of other useful information about scripts, browsers have embedded "developer tools". + +**It is recommended to use Chrome or Firefox for the development.** + +Other browsers also provide developer tools, but are usually in a "catching-up" position, compared to Chrome/Firefox which are the best. + +If there is an error in the certain browser only, then we can use it's developer tools, but usually -- Chrome/Firefox. + +Developer tools are really powerful, there are many features. On this stage let's just look how to open them, look at errors and run JavaScript commands. + +[cut] + +## Google Chrome + +Open the page [bug.html](bug.html). + +There's an error in the JavaScript code on it. It's hidden from a regular visitor's eyes, so let's open developer tools to see it. + +Press the key `key:F12` or, if you're on Mac, then `key:Cmd+Opt+J`. + +The developer tools will open on the Console tab by default. + +It looks somewhat like this: + +![chrome](chrome.png) + +The exact look depends on your Chrome version. It changes from time to time, but should be similar. + +- Here we can see the red-colored error message. In this case the script contains a "lalala" command, which was put there just because it is unknown. +- On the right, there is a clickable link to the source `bug.html:12` with the line number where the error has occured. + +Below the error message there is a blue `>` symbol. It marks a "command line" where we can type JavaScript commands and press enter to run them (`key:Shift+Enter` to input multiline commands). + +Now we can see errors and that's enough for the start. We'll be back to developer tools later and cover debugging more in-depth in the chapter . + + +## Firefox, Edge and others + +Most other browsers use `key:F12` to open developer tools. + +The look & feel of them is quite similar, once we know how to use one of them (can start with Chrome), can easily switch to another. + +## Safari + +Safari (if you use Mac, not Windows/Linux) is a little bit special here. We need to enable the "Develop menu" first. + +There's a checkbox for that at the bottom of the "Advanced" pane of the preferences: + +![safari](safari.png) + +Now `key:Cmd+Opt+C` can toggle the console. Also note that the new top menu item has appeared with many useful options. + +## Summary + + - Developer tools allow us to see errors, run commands, examine variables and much more. +- They can be opened with `key:F12` for most browsers under Windows. Chrome for Mac needs `key:Cmd+Opt+J`, Safari: `key:Cmd+Opt+C` (need to enable first). + +Now we have the environment ready. In the next section we get down to JavaScript. \ No newline at end of file diff --git a/1-js/1-getting-started/3-devtools/bug.html b/1-js/1-getting-started/3-devtools/bug.html new file mode 100644 index 00000000..edb02375 --- /dev/null +++ b/1-js/1-getting-started/3-devtools/bug.html @@ -0,0 +1,17 @@ + + + + + + + + + + There is an error in the script on this page. + + + + + \ No newline at end of file diff --git a/1-js/1-getting-started/3-devtools/chrome.png b/1-js/1-getting-started/3-devtools/chrome.png new file mode 100644 index 00000000..fd029b74 Binary files /dev/null and b/1-js/1-getting-started/3-devtools/chrome.png differ diff --git a/1-js/1-getting-started/3-devtools/chrome@2x.png b/1-js/1-getting-started/3-devtools/chrome@2x.png new file mode 100644 index 00000000..b87404a8 Binary files /dev/null and b/1-js/1-getting-started/3-devtools/chrome@2x.png differ diff --git a/1-js/1-getting-started/3-devtools/safari.png b/1-js/1-getting-started/3-devtools/safari.png new file mode 100644 index 00000000..37598a26 Binary files /dev/null and b/1-js/1-getting-started/3-devtools/safari.png differ diff --git a/1-js/1-getting-started/3-devtools/safari@2x.png b/1-js/1-getting-started/3-devtools/safari@2x.png new file mode 100644 index 00000000..c59cebef Binary files /dev/null and b/1-js/1-getting-started/3-devtools/safari@2x.png differ diff --git a/1-js/2-first-steps/01-hello-world/article.md b/1-js/2-first-steps/01-hello-world/article.md index 9ea5b652..e5555a15 100644 --- a/1-js/2-first-steps/01-hello-world/article.md +++ b/1-js/2-first-steps/01-hello-world/article.md @@ -7,7 +7,7 @@ In this chapter we'll create a simple script and see it working. ## The "script" tag ```smart header="What if I want to move faster?" -In the case if the reader has developed with JavaScript already or has a lot of experience in another language, he can skip detailed explanatins and jump to . There he can find an essense of important features. +In the case if you've developed with JavaScript already or have a lot of experience in another language, you can skip detailed explanatins and jump to . There you can find an essense of important features. If you have enough time and want to learn things in details then read on :) ``` diff --git a/1-js/2-first-steps/02-external-script/article.md b/1-js/2-first-steps/02-external-script/article.md index 78753c04..786fb276 100644 --- a/1-js/2-first-steps/02-external-script/article.md +++ b/1-js/2-first-steps/02-external-script/article.md @@ -90,7 +90,7 @@ For example, in the code below -- until all rabbits are counted, the bottom `

The behavior is called "synchronous". Usually it causes no problems, but there's an important consequence. -**If the script is external, then until the browser executes it, it can't show the page below.** +**If a script is external, then until the browser downloads and executes it, a visitor has to wait.** So, in this document, until `big.js` loads and executes, the `` content is hidden: @@ -107,21 +107,22 @@ So, in this document, until `big.js` loads and executes, the `` content is ``` -The question is -- do we really want to hide the body until the script finishes? +The question is -- do we really want the visitor to wait until the script finishes? Most of time, we don't. Sometimes, a script may contain a very important code that really must be loaded before the rest of the page is parsed (and the scripts below executed). But that's only sometimes. -Usually a visitor should see the page content while the script is loading. +Usually a visitor should be able to see the page while the script is loading. That's especially true for sites with an important content (like the tutorial) -- even if some interfaces are yet inactive (scripts not loaded yet), the visitor can read the text and navigate. There are situations when such blocking is even dangerous. For example, when we attach a script from the banner system, or a 3rd-party integration code. Like this: ```html run height=100 -Wait. The text belown will shown up only after the script executes. +Wait. The text below will shown up only after the script executes. +

…Important information!

@@ -135,8 +136,8 @@ Our first attempt could be to put all such scripts to the bottom of the `` But the solution is not perfect: -1. The script won't start loading until the whole page loads. If the page is large, then the delay may be significant. We'd like the browser to start loading a script early, but still do not block the page. -2. If there is more than one script at the bottom of the page, and the first script is slow, then the second one will have to wait for it. Browser executes only one `

…Important information!

@@ -173,12 +164,11 @@ Wait. The text belown will shown up only after the script executes. Now if we run it, we'll see that the whole document is displayed immediately, and the external script runs when it loads. -## Defer vs Async +Let's see more examples with `defer` and `async` to clearly understand the similarities and the differences. -Please note that there is one similarity and two differences between `defer` and `async`. +Both attributes allow the browser to show the page without waiting for the script to load. But... -1. **Both attributes allow the browser to show the page without waiting for the script to load.** -2. **Deferred scripts keep the relative order, while async scripts do not.** +1. Deferred scripts keep the relative order, while async scripts do not. For example, in the code below (with `async`) there are two scripts. The one which loads first will run first. @@ -198,7 +188,7 @@ Please note that there is one similarity and two differences between `defer` and This feature of "deferred" scripts is important when `2.js` relies on the result of `1.js` and we must be sure that the order is determined. -3. **A script with `defer` always waits for the HTML-document to fully load. The `async` script runs immediately as it loads.** +2. A script with `defer` always waits for the HTML-document to fully load. The `async` script runs immediately as it loads. For instance, when the document is large, like: @@ -212,25 +202,35 @@ Please note that there is one similarity and two differences between `defer` and ...Here `async.js` executes when it loads -- possibly, before the document is fully loaded. In contrast, `defer.js` always waits for the full document to be ready. - The choice between `defer` and `async` here depends on our intentions. Sometimes a script doesn't need the document at all (like a web counter), so it should execute ASAP. In this case `async` is superb. + So, if a script doesn't need the rest of the document (like a web counter), then `async` is superb. And in another case a script may need the whole document to do some work with it. Then `defer` is preferable. + + +```smart header="Either `async` or `defer`" +We can't use both `defer` and `async` on a single script. If we do that, `async` takes precedence, `defer` is ignored. +``` + +```warn header="Attributes `async/defer` are only for external scripts" +The attributes `async/defer` work only when set on a script with `src`. + +On a script without `src` like <script>...</script>, they will be ignored. +``` - And in another case a script may need the whole document to do some work with it. Then `defer` is preferable. ## Summary - Scripts in an external file can be inserted on the page via ``. -- Normally, the browser doesn't show the document after the script until it executes. Unless the script has `async` or `defer` attributes. +- The browser doesn't show the content below the script until it executes. Unless the script has `async` or `defer` attributes. - Both `async` and `defer` allow the browser to start script loading and then continue to parse/show the page. They only work on external scripts. - The difference is that `defer` keeps the relative script order and always executes after the document is fully loaded. In contrast, `async` script executes when it loads, without any conditions. Before inserting an external `