Connecting JS to HTML: From Syntax to Semantics

The hypertext standard allows you to create full-fledged HTML pages, describe them with CSS rules and fill them with functionality through JavaScript. The server forms the page and sends it to the visitor’s browser. The procedure is strictly formalized, but controlling the processes of creating and transmitting data. You can expand the limits of the possible that provide the standard and syntax for connecting JS to HTML through the "tag" tag inside the page or from an external file.

HTML and JavaScript - a natural combination

At that stage, when programming from the local one became “distributed” and went into the sphere of servicing the Internet space, it was difficult to imagine a natural combination of the impossible: a hypertext language and a programming language. Undoubtedly, there is something in common: both there and there are strict formalized syntactic constructions with restrictions on the representation and processing of the semantic moment. More dramatic differences overshadow this common:

  • the first only describes;
  • the second only processes.

At the dawn of Internet technology, the HTML page was a static, but already demanded product of the intellectual activity of the developer. JavaScript was just about to make itself known in those days. Today created and widely used:

  • Functionally complete HTML standard
  • wide range of CSS rules and features;
  • Perfect JavaScript, striving for continuous development;
  • accumulated experience of distributed processing and presentation of information.

Connecting JS and CSS to HTML is the same. The only difference is in the advisability of generating the required code in real time. That is, if the need to create a CSS style or rule during the operation of the site can still be somehow understood, then the need to create JS code for the site on which the "visitor is already sitting" can be attributed to an attack, a malicious infection, or another attempt to disrupt a peaceful existence site or damage the visitor.





Three whales of hypertext




The natural combination of the DOM element tree that the browser generates when it receives the HTML stream from the server is transformed through CSS rules into the browser viewing area and the functions described in the JS code are connected to the tree elements.

The syntax for connecting JS functionality to HTML

Essentially, JavaScript always works. The question is how much does the site developer use it. There were always two options: connecting the JS file to HTML from an external file, and by inserting the "script" tag.

Embed JS code in an HTML page




External JS files, like CSS, are included in the head (1) section. Internal script tags are placed in the body (2) section. There can be any number of script inserts, but extra zeal is impractical here. It is convenient to place JavaScript scripts at the beginning and at the end of the page, and even better, place them in external files in the amount determined by the subject area.





By naming external files in a meaningful way, connecting JS to HTML can be made clear and convenient for testing and refinement purposes.

Subtleties of connecting JavaScript scripts

Actually, the body of a function or JS variable is the static that comes from the HTML stream from the server to the browser. Function calls and the use of JS variables are dynamics that have occurred since the visitor arrived, the page loaded into the browser, and activated.

The example shows how the GoPage (3) handler is assigned to the onload event. This is a normal JavaScript function that will run as soon as the page is fully loaded. It can change the content of the page depending on who came, when came, from which domain zone, etc.

Writing code for a specific situation in JS means loading the browser and script with a lot of conventions and extra code. Localization issues and the formation of the necessary functionality is more convenient to perform on the server.

Localization issues: JS or PHP




By generating the correct code in the server language, specific functionality can be sent to the browser of a particular visitor. As a result, connecting JS to HTML will be different for different browsers, devices, visitors, their gender, age, professional interests, etc.

Frameworks, templates, CMS and other tooling systems

A good tool always contributes to a good result. There is no doubt that the popular CMS or used everywhere, often without the real need of jQuery, testifies to the highly qualified developer or simply allows you to quickly do the job.

Connecting JS to HTML in this case can create a problem. It’s one thing to do a standard set of JavaScript scripts and CSS rules for CMS, another thing is your own file, you need to know where and how to substitute it so that it is called correctly and does not create problems.

Given this point, tool developers are trying to provide the consumer with the opportunity to create site objects without access to files that store scripts, styles, or other resources. You can simply connect JS to HTML as a button event handler function. Where the body of the script gets, only the framework or CMS knows.

The team creating the site usually does not think about it, guided by the modern rule "while everything works."

Tools: CMS, jQuery, etc.




In fact, if the site is based on an advanced tool, it doesn’t matter how JS is connected to HTML.

Bootstrap, for example, was created as a free set of tools for creating sites, focusing on CSS and HTML, but now it is a powerful JS-functional. A rare developer will risk supplementing bootstrap with his ideas, the vast majority will use only its functions.

JavaScript and the "idea" of the site

Soulness and humanity are difficult to attribute to programming. Not all simple semantics can be laid down in modern syntax. There is no doubt that CMS, templates and many modern ideas are good, but they deprive the developer of the opportunity to implement their own ideas.

Soul and site idea




JavaScript - site functionality. This is the site’s actions to “perceive” the visitor and maintain an adequate dialogue with him. Leaving to the developers of CMS, frameworks or templates the contact of the site and the person is not the best solution.

When a site developer implements its own functionality (JS) and its own style (CSS) in its own HTML, it takes on its own meaning, without an external opinion, even if it is the most authoritative and qualified.




All Articles