How to create scripts. We write programs (scripts) in Windows Notepad. Creating a bat file in MS-DOS

The modern Internet is unthinkable without scripts. Learning to write in JavaScript.

If the Bible was written for the Internet, it would begin like this:

First there was the mail. And the engineers saw that it was good. And they created a WWW with hyperlinks. And the engineers saw that this is also good. And then they created the JavaScript language to animate the pages.

Something like this happened in reality. JavaScript invented to "revive" Html... Scripts JavaScript are written directly into the text Html or stored in separate fileslike styles CSS... They are executed immediately after the page is loaded into the browser.

Even the language itself was at first called LiveScript... Then it was renamed to JavaScript, because they planned to somehow link with the general-purpose language Java. But now they have practically nothing in common, and JavaScript is a completely independent programming language with its own clear ECMAScript specification.

JavaScript is technically a trademark of Oracle, and the language is an "extension" of ECMAScript, along with Microsoft's JScript and ActionScript, but this is more of a trademark problem. The main thing is that free ECMAScript does not belong to anyone.

Over time, the sphere of influence JavaScript has expanded significantly. It began to be used not only for scripts on the page. Htmlbut also for serious large web applications and entire programs that run in the browser. There are tools to package these programs in a special way and run them separately from the browser. These are compilers and interpreters, which are discussed in more detail in the training courses "" and "".

JavaScript applications run in any environment that has an appropriate interpreter.

For now, we're only interested in browsers and HTML pages.

How to make JavaScript? Writing an elementary script is no more difficult than a simple one Html-page, because scripts JavaScript are written in plain text, that is, they can be created literally in the same "Notepad", then saved in separate files or inserted into the body Html-document. The simplest things on JavaScript are really simple.

How to write JavaScript

As an example, let's make a simple script to be executed by the Windows Script Host. 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 in "Explorer" Windows.

A similar script can be written directly in the page code Html between tags . There you can already use regular JavaScript methods, not the method echo specific object WScript... Let's look at some of the standard methods for input and output in a browser.

alert ()

Method alert () displays a window with an "OK" button. The window displays a message that is indicated in brackets. For instance, "Hello Skillbox!"... That is, in this case, the browser does exactly the same thing that the script server did before. Windows.

These examples can also be written in Notepad, only saved in files with the extension Html... For instance, skillbox.htm.

<html>
<script>
alert (" Hello Skillbox")
script>
html>

Result:

As an argument alert () you can specify not only a specific text, but also the result of any calculations or processing of other data. For instance, alert (x)where x is calculated separately.

confirm ()

Method confirm () displays the same window with a message, but with two buttons - "OK" and "Cancel". Depending on which button the user clicks, the method returns either a value trueor false... The server receives this return value from the user and performs some action based on the response.

The syntax is the same, only choice is logically assumed here, so the user is asked a question.

<html>
<script>
confirm (" Hello Skillbox")
script>
html>

Result:

prompt ()

Method prompt () displays a dialog box with a message and a text box where the user enters data. There are also two buttons "OK" and "Cancel". By pressing the first button, the method returns the entered text to the server, and by pressing the second button it returns boolean false.

The syntax here is:

prompt ( message, data_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.

<html>
<script>
prompt (" Say hello to Skillbox", "Hello")
script>
html>

Result:

The possibilities of modern JavaScript go far beyond primitive form I / O. We have given these methods only as the simplest examples. Besides, JavaScript allows you to react to user actions. For example, by moving the mouse or pressing certain keys. JavaScript often used to provide asynchronous work ( Technology AJAX) when the information on the page is refreshed without reloading it. In this mode, data is sent to the server and loaded from there interactively. Besides, JavaScript able to manipulate Html-elements on the page (create and hide tags, etc.) and do much more.

Useful tools

Developer Console

All popular browsers have a dedicated developer console. It shows the script code on the page, as well as displays other useful information. IN Chrome, Firefox and IE developer console opens on click hotkey F12, in Safari - Ctrl + Shift + I or Ctrl + Alt + C. In the screenshot, the scripts are displayed in the upper right, along with other elements of the web page.

Chrome developer console.

Code editors

In the future, for convenient programming, you will need to install a code editor or IDE (Integrated Development Environment), integrated development environment. IDE is an editor with advanced functionality that is integrated with other useful tools, supports the connection of additional modules, and so on.

(training course for beginners)

INTRODUCTION

The main goal of the course is to learn how to write scripts on the Internet for a wide variety of applications and learning. A script (script, dialogue) is a kind of simulator in which you can trace:
- what data is received with the possibility of changing it,
- what parameters are calculated,
- the relationship of input and output parameters by the nature of the displayed graphs,
- dismember the task into its components with the construction of hypersections,
- by going into the script codes, read the mathematical code of the problem,
- due to the object-oriented principle of programming (methods), attract other programs to work in the script,
- organize the work of scripts with databases and nomograms,
- organize an exit from the script to other powerful applied systems (for example, the "Vector" system), where it is more professional to solve the problem and, if necessary, return to a new quality level.

What is said above is supported by the results of the work done on the pages of the site:
150 scripts, Know-Nau, study of academic disciplines with test cards of control and learning.

The purpose of this course is to use different techniques: passive (for the lazy, busy), active, creative - learn how to write scripts.

Initial training:
- reading (acquaintance) with help information,
- simple mechanical "stuffing" of tests (listings) of scripts - this form of mastering scripting is useful and should not be avoided.
- debugging of typed scripts - here you have to show patience, perseverance and inquisitiveness to ensure that the script "works" for you.
- analysis of existing scripts - in the test-cards of training and control: using questions to analyze what the script does.
- writing a script not mechanically, but according to the task at hand,
- and finally, writing a personal script or scripts - this will already be aerobatics and if you have done this, then you can safely submit the script to the competition.

Where to start writing scripts?

First, let's figure out what a script is?
A script is a script (dialog, macro).

The script includes everything: setting input and output parameters, performing an action (dialogue), viewing information in digital and graphical forms, audio and video accompaniment, combined in one word - in multimedia, etc.

How do I create scripts?

There are many programs that you can use to write scripts, and they have one thing in common - they are written for the Internet. Working with the Internet (viewing Web pages, etc.) is carried out by a browser - a program suitable for viewing HTML files. There are many such programs, but the most common Microsoft Internet Explorer - it is installed on every computer and therefore a beginner does not need to know about others. Another important property of browsers is that they can not only view HTML files, but also debug them, and not on the Internet, but locally on a computer that is not connected to the Internet.

The most simple page (consider the simplest script) is created using the HTML system, which is nothing more than a text-based coding system used to tell the browser (in our case Internet Explorer) how to display information. There are many ready-made systems that allow you to create HTML files that the user, in principle, does not need to get into. However, in order to work with JavaSript - where scripting is no longer necessary - the ability to work with text, creating tables, placing other tables, input and output variables in them, forming the "Run", "Draw" buttons, etc. - knowledge of elementary HTML codes and the elementary ability to work with them is necessary. When studying, in principle, one lesson is enough for this - then use Help.

However, to write the same full-fledged scripts, something else is required.
And that "something" is also a full-fledged scripting program JavaScript, without which it is impossible to create a script (read and web page). Here knowledge and skills are already needed throughout the program. A huge number of scripts have been written, unfortunately all of them are for Web-pages in their pure form, and almost none for engineering, artistic and creative and even more everyday ones.
Truth, ready-made scripts can be used as templates, or as some part of your script, but all this requires adjustments, which will take more time than writing a new script.
Learning JavaSript is not difficult, and even novice programmers can put together several scripts that can impress not only friends, but yourself.

Graphic arts

It would be possible not to raise the question of how to learn how to write scripts. In the Internet
and on the shelves of stores there are a huge number of books, manuals and programs, CDs - if not for their weak point - there is no (or almost no) opportunity to work with vector graphics!
The reason is that vector graphics, on the one hand, are still beyond the power of the Internet, and on the other hand, application programs with vector graphics are focused on professional design and manufacturing tasks, do not yet solve the problems posed by the Internet and, in particular, script programming.
To be continued…

Many aspiring web programmers become familiar with the language through books. The book is indisputably very important and necessary, but it provides examples (for beginners) that are inherently not correct in terms of the stability and portability of the script. Stability refers to the possibility of errors of various kinds, under portability - the same, but when transferring the script from the home local machine to the hosting provider's server.

Below, I would like to immediately show novice web developers how to learn how to write scripts right away, so as not to experience unnecessary problems and not "catch" errors that are not clear where they come from.

I must say right away that I faced this myself, so all this went through my torment ....

I will consider the following points:


2. Where is the "@" buried?
3. register_globals \u003d Off and nothing else.
4. The main aspects of syntax. Literal op not literal ...

1. Setting up PHP for the local machine and on the server.

Why do I start with this chapter. The fact is that many on the local machine immediately disable the display of all errors. It is not right. Instead of stupidly looking at the monitor and looking for where, as it turns out, the semicolon is missing, you can immediately understand what the matter is by an error issued by PHP.

But first, I want to make a small lyrical digression on how to configure PHP, like Apache modul or fast-CGI.
The best option is to configure it as a web server module. To do this, write the following lines in the httpd.conf configuration file of the Apache web server:

LoadModule php4_module D: /bin/php/sapi/php4apache2.dll AddType application / x-httpd-php .php .phtml .php4 .php

The difference from FastCGI is as follows. First. With FastCGI, php.ini will be loaded into the memory of the web server approximately every second time the script is run, when installed as an Apache module, the PHP configuration will be loaded only when the web server is loaded or restarted, of course, the speed and load on the server will be less. In addition, when installed as fastCGI are not available certain functions... A novice programmer will not need them, but when developing a large Internet application, you can run into problems. And problems are better prevented.

Now let's move on to configuring PHP itself. In the php.ini configuration file, we must set the following directives with the values \u200b\u200bwritten below:

error_reporting \u003d E_ALL display_errors \u003d On display_startup_errors \u003d On

Whether or not to write errors to the log files is up to you.
On the server in the hosting company, be sure to turn off all these directives for security purposes, so that a potential attacker could not find out the paths where your scripts are located. But for good, when you already publish your script in global network, it shouldn't throw any errors.
Besides

short_open_tag \u003d Off In order not to be able to use

By default, the max_execution_time directive is set to 30, but for faster work, it is better to set less. If you have something "looped", then 10 seconds is enough to understand it.

register_globals \u003d Off register_argc_argv \u003d Off

In Chapter 3 I will go into more detail about this ...

magic_quotes_gpc \u003d Off

Very often, novice web developers do not understand the difference between 'and “, so turn off the directive. In chapter 4 this moment will be shown with an example ...

2. Where is the "@" buried?

This is perhaps the most common mistake made by novice programmers - using “@”. This symbol, before any operator, suppresses the error output and its logging. It does not allow you to trace the error at the debugging stage, and you frantically try to figure out what the matter is.

A striking example. Suppose we put an “@” in front of the line $ f \u003d fopen (“fle.txt”, “w +”) ;. If we take only the most common errors that may arise during subsequent fwrite, for example, then these are: a) no access rights to the file, b) the file does not exist, c) fopen cannot be called in safe mode. Can you imagine how many variants of errors there can be? A lot. And how are you going to find out which error occurred for you, because you suppressed their output with the "@" symbol.

Therefore, we strictly forbid you to use @.

You ask, how then can you do so that an error is not displayed?
In order not to display an error, it must either be prevented, as is the case with files, that is, a) check the file for existence, b) check, or you can (have rights) write something to it is_writeable () ;.

In other cases, such as when using mysql_connect (); it is necessary to check the value returned by the function. As a rule, it can be used to determine whether there is an error or not. Typically, an error will return FALSE, NULL, or an empty string.

This will allow you to give the user not an empty page if, for example, he tried to access information (? N \u003d 14), and the information is stored in your files, and file 14 does not exist, and the text is a message: "Information not found." ...

"Catching" errors is also a very important aspect of script safety.

3. register_globals \u003d Off and nothing else.

If in the first two chapters I focused on those moments that a beginner programmer will face immediately, then in this chapter I will talk about a moment that will appear later ... often everyone encounters it when transferring a script (already ready) to a hosting server provider. Personally, I got used to register_globals \u003d Off for a long time, so comrades, novice programmers, immediately set this directive off. In addition, this moment is VERY IMPORTANT in ensuring the security of the script.

So. What is the whole stag. Let's first consider the case where we have register_globals \u003d On. As you probably already know, data can be transferred to a script in three ways, he said roughly, in fact there are two of them. The first one is GET, i.e. data is transmitted through the address bar of the browser after the “?” symbol, the second - POST, when using it, the data is transmitted in an implicit form for the user. The POST method is typically used to submit form data. And the third one is the Cookies transmitted from the user to the script.

Consider this case. Do we have a form located at index.php? form. There are multiple text fields in the form. Let it be family, name, phone. The form is sent to the same index.php but with the POST method. After checking all the data, we wrote down two Cookies to the user with the names family and form, in the second we recorded the time of access to the form, for example.

Let's imagine that part of the index.php file consists of the following code, i.e. if the user has already entered data on the form, they are shown to him, if not, the form is displayed. And all this at index.php? Form.

"; print" You were here: ". $ form."
";) else (// Show our form.))?\u003e

Now think about it .. What will be in $ form, because we are passing the variable as a GET parameter or via a cookie. Here. Further, you will frantically search for an error in why you have $ form empty or vice versa.

Those. in fact, it all comes down to the fact that it is possible to substitute all variables. In fact, this is a significant disadvantage in security, since there is a very high probability that an attacker will be able to view any important files on the server.

Now how to implement this under register_globals \u003d Off

There are several global arrays in PHP. $ _GET, $ _POST, $ _COOKIE, $ _REQUEST (combining the first three, not recommended for security reasons), $ _FILES (for uploading files), $ _SESSIONS (sessions), $ _SERVER (server variables), $ _ENV (environment variables) , $ GLOBALS (combines everything).

What does it mean. Below I will simply rewrite the script I gave earlier for register_globals \u003d Off ..

"; print" You were here: ". $ _COOKIE [" form "]."
";) else (// Show our form.)?\u003e

Now there will be no problems.
Just in case, I'll clarify that in $ _ **** [‘name’], instead of name, you must write the name of the text field or Cookie or the parameter passed from the address bar.

In this chapter, I want to consider the following small example. Again, let's first look at an example with register_globals enabled.

The script will display the value of $ a equal to 7. That is, in fact, we have variables available both inside and outside the function. This is not good, because with a large script there can be a lot of such $ a variables, and as a consequence, in any function, the value of the variable can be changed and the result of the script execution will be unpredictable.
Now, if the same code is executed with register_globals \u003d Off, the output will be 2. Since changes to the $ a variable inside the function will not affect the $ a variable in the main body of the script. Here you need to read the manual about the scope of variables.
If we need to get this seven, then we need to return the local $ a value from the function and store this value in the global $ a.

4. The main aspects of syntax. Literal op but literal ...

1. Again, many novice programmers do not understand the difference between writing: $ _POST [‘pole’] and $ _POST. The first option is syntactically correct, while the second is not. PHP will try to find not a pole element, but an element with the name stored in the constant pole, which you don't have.

Today, working with information is quite common, even for those who are not directly involved in journalism. The point is that today everyone regularly uses the Internet to obtain relevant information on a specific request.

However, this work is not always carried out in an efficient manner. Of course, the optimization and automation of working with information in the browser primarily concerns professionals.

Meanwhile, useful aspects of the above processes can be recommended for ordinary users. Insofar as they can be applied without any special knowledge. The Human Emulator program makes it possible to independently write scripts to work in the browser.

A script is a program code that allows you to automate an action that is repeated regularly. In fact, the script will perform the action exactly as if it were performed by a person personally.

However, until recently, the solution to the above problem was the prerogative of exclusively professional programmers. Today, software has appeared on the market (free, by the way), which allows you to do everything the same, only without knowing the programming language.

The advantages of automating work in the browser are obvious:

  • increasing the speed of working with information;
  • reduction in the number of errors;
  • ensuring high positive results in work.

The point is that any work with information has some kind of templates. Regardless of the direction of the information, these patterns are repeated. It is they that allow you to efficiently collect, accumulate, organize and process information arrays.

Automation is the path to success

Analyzing a large amount of information always leads to significant losses in time. But the main problem is still errors. Of course, a person is a person.

The human factor cannot be excluded when processing and analyzing information. A script is a program code that carries out the execution of a given order of actions without error.

Note that to solve complex problems, you will still need to know the basics of the programming language.

The video will demonstrate an example of automating work in a browser: