For example, let’s make a simple script to be executed by the Windows Script Server. This script can be written directly in Notepad and executed without a browser.
We write this text in Notepad, then save the file under the name skillbox.js and run it in Windows Explorer.
The alert() method displays a window with an OK button. A message is displayed in the window, which is indicated in brackets. For example, “Hey Skillbox!”. That is, in this case, the browser does exactly the same thing that the Windows Script Server did before.
These examples can also be written in Notepad, but saved in files with the extension HTML. For example skillbox.htm.
As an argument to alert(), you can specify not only a specific text but also the result of any calculations or processing of other data. For example, alert(x), where x is calculated separately.
The confirm() method displays the same message box but with two buttons – “OK” and “Cancel”. Depending on which button the user clicks, the method returns either true or false. The server receives this return value from the user and performs some action based on the response.
The syntax is the same, only a choice is logically assumed here, so the user is asked a question.
The prompt() method displays a dialog box with a message and a text field where the user enters data. There are also two buttons “OK” and “Cancel”. On pressing the first button, the method returns the entered text to the server, and on pressing the second button, it returns the boolean value false.
The syntax here is:
The input field value is optional. There you can enter the text that was originally entered in the field for the convenience of the user.
All popular browsers have a dedicated developer console. It shows the script code on the page and also displays other useful information. In Chrome, Firefox, and IE, the developer console is opened by pressing the hotkey F12, and in Safari – Ctrl+Shift+I or Ctrl+Alt+C. In the screenshot, the scripts are displayed at the top right, along with other elements of the web page.
In the future, for convenient programming, you will need to install a code editor or IDE (Integrated Development Environment), an integrated development environment. An IDE is an editor with extended functionality that is integrated with other useful tools, supports connecting additional modules, and so on.
For starters, we can recommend one of the light editors:
- Sublime Text ;
- Atom ;
- Sci Te ;
- Notepad++ .
In the future, it makes sense to take a closer look at the IDE :
- IntelliJ WebStorm ;
- Visual studio.