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.
WScript.echo (“Hello Skillbox!”)
We write this text in Notepad, then save the file under the name skillbox.js and run it in Windows Explorer.
A similar script can be written directly in the HTML page code between the tags. You can already use normal JavaScript methods there, rather than the echo method of a specific WScript object. Let’s look at some of the standard methods for input and output in a browser.
alert()
The alert() method displays a window with an OK button. A message is displayed in the window, which is indicated in brackets. For example, “Hi, 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 as files with the HTML extension. 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.
confirm()
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.
prompt()
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:
prompt(message, input_field_value)
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.
The code:
The possibilities of modern JavaScript go far beyond the primitive input / output of data through forms. We have given these methods only as the simplest examples. In addition, JavaScript allows you to respond to user actions. For example, on mouse movements or pressing certain keys. JavaScript is often used to provide asynchronous work (AJAX technology), where information on the page is updated without reloading it. In this mode, data is sent to the server and downloaded from there interactively. In addition, JavaScript is capable of manipulating HTML elements on a page (creating and hiding tags, etc.) and much more.
Useful Tools
Developer Console
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 F12 hotkey, 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.
Code editors
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.