How to set up smartphones and PCs. Informational portal
  • home
  • Safety
  • JavaScript programming language (Java Script). What is Javascript and where is it used?

JavaScript programming language (Java Script). What is Javascript and where is it used?


Today, there is probably no website that is not written using javascript.

The use of this script has become so popular that it is now simply impossible to do without it. The only people more popular and famous than javascript today are probably Paris Hilton and McDonalds.

What exactly is he?

Javascript is a scripting language designed to create interactive web pages. It works as is, without requiring compilation, but simply connecting to the HTML page. It should not be confused with Java - even if the names of these two languages ​​are similar, this does not mean that they are functionally related. In fact, it is much closer, for example, to Ruby or Python, but has its own ECMAScript standard, and therefore is still separate from them.

What Javascript Can Do

Javascript can do a lot - for example, it can be used to easily change element styles, remove tags, or add new ones. In this case, you can set the function in such a way that the script reacts to certain events - either mouse or key clicks, or begins to act on its own after some time has passed after loading. And with AJAX you can get updated data without reopening the page.

Javascript is capable of setting and reading cookies, displaying messages, windows, validating data, and much, much more. Handyman.

Javascript has three main advantages, including, for example, accessibility (by learning the basics of JavaScript, you can easily understand most of them and always improve your skills); universal support (they are supported in almost any computer environment - any modern computer is capable of this), as well as full browser integration, making javascript even more convenient and easy to use.

As an example, we can cite technologies like VBScript, XUL and ActiveX - they are not supported in every browser and only work in a certain environment.

And of course, we must not forget about the speed and efficiency of JavaScript, which makes many programming languages ​​pale in comparison.

It would not be amiss to note that even now this technology continues to develop, and at a rapid pace - just have time to keep up! And soon a new interpreter will be created along with javascript 2 technology.

Advantages

JavaScript has many advantages, otherwise it would not be used so actively. It earned the love of users for a reason. One of its main advantages is considered to be interactivity, which is why most sites are written in this language. This is achieved thanks to the AJAX tool, and with its help the data will update itself, without user interaction. This is very convenient, for example, on payment system websites. This will be very useful for not the most experienced users - real help from the script itself that you use. What could be better?

Now I’ll tell you a little about the structure of this language.
  • JavaScript has several features, including the following:
  • Identifiers are case sensitive;
  • Variable names can include the "$" symbol, underscores, numbers (but cannot begin with them), and letters;

JavaScript is one of the programming languages ​​that is commonly used to programmatically access application objects. It is often built into the browser and is necessary to make web pages more functional. Accordingly, if JavaScript is disabled for one reason or another, the page may not open at all or will open, but with significant limitations or errors. For example, certain text may not be shown. Therefore, if you do not know how to activate this language in your Internet browser, then you have come to the right place.

Mozilla Firefox
  • In the top menu, select the “Tools” - “Settings” section.
  • A window has opened in front of you on the “Basic” tab. You must select the "Content" tab.
  • At the top of the window you will see several items, one of which will be called “Use JavaScript” - check the box next to it and click OK.

  • Refresh the page using the F5 key on your keyboard.

ATTENTION! This description is only suitable for versions below version 22. Starting from version 23, JavaScript is not required - the option is enabled by default.

Opera
  • In the upper left corner, click on the button “Tools” - “Settings” - “General settings”.
  • Once in the menu, go to the “Advanced” tab (it’s the fifth one).
  • In the left menu, click on the “Content” link and next to “Use JavaScript” check the box.

  • Click OK and refresh the page.
Google Chrome
  • In the upper right corner there is a button on which you will see three bars - click on it and select the “Settings” section.
  • Scroll down the page to the very bottom and click on “Show advanced settings”.
  • In the “Personal Information” section, you will see a “Content Settings” button that you need to click on.
  • An additional window has opened. Find the option “Allow all sites to use JavaScript (recommended)” and click OK.

  • Reload the page in your Internet browser.
Internet Explorer
  • Open the browser properties (this can be done either in the browser itself through the menu or using the control panel of your computer).
  • A window has opened, you need to go to the “Security” tab.
  • At the bottom of it you need to click on the button labeled “Other...”.
  • A large list has opened. You need to go to the “Scripts” section and in the “Active Scripts” subsection, check the box next to the “Enable” item.

  • Click OK and refresh the page.
Apple Safari
  • In the menu we find the “Settings” section.
  • A separate window has opened with several tabs, among which you need to open the one called “Security”.
  • Next to the “Enable JavaScript” item, check the box and close the window.

  • We refresh the page by pressing the F5 key.

It should be noted that all versions of the most famous and popular browsers today provide support for JavaScript. They support the third edition of the specification, and Mozilla Firefox even attempted to support the fourth edition. By default, JavaScript is used by default on almost all Internet browsers, and it is usually disabled by the user himself.

As for the use of the language, it is necessary, for example, in some web applications, scripts in browsers (we are talking about special programs that allow you to format pages, automatically fill out forms, hide part of the content, and so on), as well as in AJAX ( an approach to building application interfaces in which data exchange with the server occurs in the background, due to which the page is not completely reloaded, and the operation of the application itself only speeds up).

Previously, I did not think that the Internet could provide such extensive opportunities for developing my business, but time passed and I finally discovered this world full of opportunities. The Internet provides an opportunity to sell your products, a wide variety of them. Every day the number of users of the World Wide Web is growing, and at the same time our capabilities are growing.

Once upon a time I did not understand many of the earning schemes, but today I can say with complete confidence that the basis of any business on the global network is a website. Moreover, not just a site, but the right site. Moreover, many factors influence whether the site will be profitable. The first thing a visitor to any website encounters is the interface.

Believe me, how attractive the site is will determine whether a casual user will stay on it or not. Of course, if a person has been reading your site for a long time, then he will forgive you for various mistakes that you may make in the functionality of the site. But for the first time, the interface will play a key role in making the client like your website. As they say, “they are greeted by their clothes, they are seen off by their minds.”

But in order to make original and memorable clothes for your site, you will need to know what javascript is for. In fact, without JavaScript it is impossible to create a beautiful, functional website. Moreover, there will be no need to clutter the site with tons of code. It will be enough to add just a couple of lines, and various buttons and menu items will “come to life” and become more attractive. By the way, a JavaScript script will allow you to scroll through pictures in your portfolio. In addition, you can further increase the functionality of the page when displaying data. JavaScript is also needed here

Perhaps many may think that, for example, it works on a good CMS, and to manage such a blog you do not need JavaScript knowledge at all. On the Internet you can find many different modules and plugins with which you can significantly improve functionality, add cool effects, etc. However, we should not forget that web technologies do not stand still, everything is constantly changing. Therefore, it is worth constantly monitoring the emergence of new JavaScript libraries, because it is possible that today's packages will make it possible to realize what only yesterday seemed just a dream. That’s why when they talk about updating a site, they also mean the use of JavaScript.

Now for learning JavaScript. For some reason, many people believe that learning this programming language is only available to a select few. But actually it is not. Web programmers are constantly working to create new JavaScript elements that greatly simplify its syntax and use. In addition, you can use JavaScript code on the pages of your site if you know how to use the appropriate manuals. Wonderful video courses will come to the rescue with this, with the help of which even a complete layman can master JavaScript in a short time.

I would like to express my gratitude to Evgeniy Popov, the author of a number of video courses on website building, for information support. Including the video course “Javascript + jQuery for beginners in video format”

In this article we will tell you what JavaScript (Java Script) is, give examples, and explain why it is needed in html code. We will provide answers to popular questions from webmasters.

1. What is JavaScript in simple words

JavaScript (in Russian "Java Script", abbreviated as "JS") is a programming language built into HTML pages for the purpose of convenient communication with the user, error handling and creating dynamism of the page as a whole.

JavaScript is an object-oriented and interpreted language. It is system independent (works on any platform). JS code is often called scripts. We can say that this language is “tailored” only for web programming.

The syntax of the language is in many ways reminiscent of C and Java, but semantically the language is much closer to Self, Smalltalk.

2. Problems solved by JavaScript
  • Validation of html form fields (tag) before they are sent to the server
  • Informational messages with warnings
  • Effects for highlighting main elements and parts of the page
  • Original drop-down menus
  • Mathematical calculations

Sliders are usually made using JavaScript. You can also display content via AJAX output, this allows you to hide unnecessary or repeated elements of a site page from the html code. It makes no sense for a beginner to study these codes, because... they are quite complex.

3. How to enable JavaScript in your browser

All browsers support JavaScript. By default, Java Script should already be enabled in browsers, if not, then below are instructions for various browsers on how to enable JS.

1. Enable JavaScript in Firefox 2. Enable JavaScript in Google Chrome

"Customize and manage Google Chrome" -> "Options" -> "Advanced" -> "Personal information (Content settings)" -> "JavaScript:" Allow all sites to use JavaScript (recommended) or disable JavaScript on all sites

3. Enable JavaScript in Opera

Press the F12 key Select "Enable JavaScript"

4. Enable JavaScript in Internet Explorer

"Tools" -> "Internet Options" -> "Security" tab -> select the "Internet" zone -> "Other" button -> "Scripts" -> "Allow scripts"

4. Example: how to insert JavaScript into an html page

The JavaScript code fits directly into the html code of the page. To highlight it, use special tags

javascript code

The type="text/javascript" attribute is not required, but recommended.

You can use ‹script› in title tags or simply in the body of the page.

It is prohibited to write HTML tags between the opening and closing script tags. This place is for JavaScript code only.

To avoid duplicating the same JavaScript on each page, it is usually placed in a separate file with the extension ".js". This file can be included anywhere in the html page.

4.1. Example 1: Hello, World Pop-up Message

Below is an example of html code that displays a pop-up message "Hello, World" when the page loads.

Hello, World in JavaScript

Example with Alert. A pop-up message will be displayed

alert("Hello, World!");

Click on the link:

Link4.2. Example 2. Alternative with javascript processing

Below is an example of html code with JavaScript code processed using the id identifier.

Hello, World in JavaScript window.onload = function() ( var linkWithAlert = document.getElementById("check_link"); linkWithAlert.onclick = function() ( return confirm("Follow the link? "); ); );

Click on the link :

Link4.3. Example 3. Changing the color of an object on hover

When you hover your mouse over certain areas, they change color. This is done through JavaScript. For example

Code for this example:

RED GREEN BLUE

This is just the most basic JavaScript stuff. Its capabilities are quite large. It’s not for nothing that you can find separate books on this language in literary stores.

The JavaScript programming language is an object-oriented scripting language originally developed by Netscape Communications under the name LiveScript, but later renamed "JavaScript" and with a syntax closer to Sun Microsystems' Java. JavaScript was later standardized by ECMA under the name ECMAScript. Microsoft calls its versions JScript.

The change in name from LiveScript to JavaScript occurred around the same time that Netscape included support for Java technology in the Netscape Navigator browser. This change has created a lot of confusion in the minds of those learning to program for beginners. There is no real connection between Java and JavaScript; their similarities begin and end with similar syntax and the fact that both languages ​​are widely used on the Internet.

JavaScript is an object-oriented scripting language that interacts through an interface called the Document Object Model (DOM) with content that can be executed on the server side (web servers) and on the client side in the user's web browser when viewing web pages. Many websites use client-side JavaScript technologies to create powerful dynamic web applications in programming for dummies. It can use Unicode and can use the power and strength of regular expressions (this was introduced in version 1.2 of Netscape Navigator 4 and Internet Explorer 4). JavaScript expressions contained as a string can be executed using the EVAL function.

One of the main tasks for JavaScript is small functions embedded in HTML pages that allow you to interact with the DOM from the browser to perform certain tasks that are not possible in static HTML: such as opening a new window, validating values ​​entered into a form, changing the image on hover. mice, etc. Unfortunately, creating such functions is quite tedious because browsers are not standardized, different browsers can create different objects or scripting methods, and so you often have to write different versions of a JavaScript function for different browsers, but this is not very convenient when learning the basics of programming.

JavaScript / ECMAScript is supported by such engines as:

  • Rhino
  • SpiderMonkey

Environment

The markup comment is required to ensure that the code does not display as text in browsers that do not recognize the . tags in XHTM/XML documents, however, will not work if commented out. Modern browsers that support XHTML and XML are well designed enough to recognize , so the code in these documents remains uncommented.

An HTML element can generate internal events to which a script handler can be connected. To create a valid HTML 4.01 document, you must insert the appropriate default script link statement in the document head section.

Elements of language

Variables

Variables are usually dynamically typed. Variables are defined either by simply assigning a value to them or by using the "var" operator. Variables declared outside a function are in "global" scope, visible throughout the web page; variables declared inside a function are local to that function. To pass variables from one page to another, the developer can set a "cookie" or use a hidden frame or window in the background to store them.

Data structures

The main type is an associative array data structure similar to hashes in the Perl programming language or Python, Postscript and Smalltalk dictionaries.

Elements can be accessed by numbers or associated names (if these have been defined). Thus, the following expressions may all be equivalent:

MyArray,
myArray.north,
myArray["north"].

Declaring Arrays

MyArray = new Array(365);

Arrays are implemented so that only certain (non-empty) elements will use memory, they "discharge the arrays". If we set the set myArray = "something there" and myArray = "something else there", then we have used space only for these two elements.

Objects

JavaScript has several kinds of built-in objects, namely Object, Array, String, Number, Boolean, Function, Date and Math. Other objects belong to DOM objects (windows, forms, links, etc.).

By defining constructor functions, you can define objects. JavaScript is a prototype-based object-oriented language. You can add additional properties and methods to individual objects after they have been created. To do this, you can use a prototype statement for all instances of a particular type of object.

Example: Creating an Object
// Constructor function

Function MyObject(attributeA, attributeB) ( this.attributeA = attributeA this.attributeB = attributeB )
// Create an object
obj = new MyObject("red", 1000)

// Access an object attribute
alert(obj.attributeA)

// Access attribute with associative array designation
alert(obj["attributeA"])

The hierarchy of objects can be reproduced in JavaScript. For example:

Function Base() ( this.Override = _Override; this.BaseFunction = _BaseFunction; function _Override() ( alert("Base::Override()"); ) function _BaseFunction() ( alert("Base::BaseFunction()" ); ) ) function Derive() ( this.Override = _Override; function _Override() ( alert("Derive::Override()"); ) ) Derive.prototype = new Base(); d = new Derive(); d.Override(); d.BaseFunction();

As a result, we get on the screen: Derive::Override() Base::BaseFunction()

Control instructions
If ... else if (condition) ( statements )
Cycles
while (condition) ( statements ) Do ... while do ( statements ) while (condition); For loop for (; ; ) ( statements ) For loop ... in
This loop goes through all the properties of an object (or element in an array)
for (variable in object) ( statement )

Selection operator
switch (expression) ( case label1: statements; break; case label2: statements; break; default: statements; )

Functions
The body of the function is contained in (the body can be empty), and the list of arguments is indicated inside () following the function name. Functions can return a value after execution.

Function(arg1, arg2, arg3) ( statements; return expression; )

As an example, let's look at a function based on the Euclidean greatest common divisor algorithm:

Function gcd(a, b) ( while (a != b) ( if (a > b) ( a = a - b; ) else ( b = b - a; ) ) return a; )

The number of arguments when calling a given function does not necessarily have to provide as many arguments as were specified when the function was declared. In a function, arguments can also be accessed through an argument array.

Each function is an instance of a function, a base type of object. Functions can be created and assigned like any other objects:

Var myFunc1 = new Function("alert("Hello")"); var myFunc2 = myFunc1; myFunc2();

Result on screen:

User interaction

Most user interaction is done using HTML forms, which can be accessed through the HTML DOM. However, there are also some very simple means of communicating with the user:

Alert dialog box
Confirm dialog box
Dialog lines
Status bar
Consoles

Text elements can be the source of various events, which can trigger actions if an EMCAScript event handler is registered. In HTML, these event handler functions are often defined as anonymous functions directly in the HTML tags.

Best articles on the topic