In maths, one would write xn = x * xn-1.… pow(2, 1).The process repeats: a new subcall is made at line 5, now with arguments x=2, n=1.… pow(2, 1).… It has the result of the subcall pow(2, 1), so it also can finish the evaluation of x * pow(x, n - 1)… In the HTML document, an HTML-tag may contain a list of:
Text pieces.
HTML-comments.
The backbone of an HTML document is tags.… According to the Document Object Model (DOM), every HTML tag is an object.… For instance, the top tag is always <html>.… > directive at the very beginning of HTML is also a DOM node.… …etc, everything in HTML has its place in DOM, even comments.
Usually the HTML-mode is used for webpages.… ="more html".… HTML of the element.The outerHTML property contains the full HTML of the element.… It puts the new HTML in its place instead.… With innerHTML we’ll have it inserted “as HTML”, with all HTML tags.
Many built-in objects already exist, such as those that work with dates, errors, HTML elements, etc.… It would be great to access them using methods.… The language allows access to methods and properties of strings, numbers, booleans and symbols.… So in the moment of accessing its property, a special object is created that knows the value of the string… Java allow us to explicitly create “wrapper objects” for primitives using a syntax like new Number(1)
The most deeply nested element that caused the event is called a target element, accessible as event.target… Normally it goes upwards till <html>, and then to document object, and some events even reach window… If you click on <p>, then the sequence is:
HTML → BODY → FORM → DIV -> P (capturing phase,… the first listener):
P → DIV → FORM → BODY → HTML (bubbling phase, the second listener).… Each handler can access event object properties:
event.target – the deepest element that originated
This is the correct variant:
Resulthello.jsuser.jsindex.htmlIn the browser, if we talk about HTML pages… wait until the HTML document is fully ready (even if they are tiny and load faster than HTML), and then… As a side effect, module scripts always “see” the fully loaded HTML-page, including HTML elements below… Async scripts run immediately when ready, independently of other scripts or the HTML document.… “Special” module types like HTML/CSS modules are also supported.
Please don’t use id-named global variables to access… That’s fine for simple scripts, inlined into HTML, but generally isn’t a good thing.… Also, when one reads JS code and doesn’t have HTML in view, it’s not obvious where the variable comes… the collection with elements that the pointer is over now (in nesting order: from the outermost <html… As of now, its length is 1.
For example, we need that to store a list of something: users, goods, HTML elements etc.… We can explicitly calculate the last element index and then access it: fruits[fruits.length - 1].… The square brackets used to access a property arr[0] actually come from the object syntax.… Move all elements to the left, renumber them from the index 1 to 0, from 2 to 1 and so on.… becomes "1" and [1,2] becomes "1,2".
On the teststand below, all mouse events are logged, and if there is more than a 1 second delay between… event.button are:
Button state
event.button
Left button (primary)
0
Middle button (auxiliary)
1… property that’s an old non-standard way of getting a button, with possible values:
event.which == 1… Surely the user has access to HTML-source of the page, and can take the content from there, but not everyone
JavaScript can access an existing selection, select/deselect DOM nodes as a whole or partially, remove… As we can see, this phrase consists of exactly two children of <p>, with indexes 0 and 1:… imply that there may be many ranges, but again, in all browsers except Firefox, there’s at maximum 1.… As an input value is a pure text, not HTML, there’s no need for such objects, everything’s much simpler… DOM spec: Range
Selection API
HTML spec: APIs for the text control selections
Summary.We covered two
chapter Blob),
"document" – get as XML document (can use XPath and other XML methods) or HTML… The current state is accessible as xhr.readyState.… An XMLHttpRequest object travels them in the order 0 → 1
But for binary processing, to access individual data bytes, we can change it to "arraybuffer"… socket.readyState property with values:
0 – “CONNECTING”: the connection has not yet been established,
1… HTML: we need a <form> to send messages and a <div> for incoming messages:
A built-in <template> element serves as a storage for HTML markup templates.… The browser ignores its contents, only checks for syntax validity, but we can access and use it in JavaScript… In theory, we could create any invisible element somewhere in HTML for HTML markup storage purposes.… First, its content can be any valid HTML, even if it normally requires a proper enclosing tag.… We can access template.content from JavaScript, clone it to reuse in a new component.
A number is a sequence of 1 or more digits \d.… Improved: /<[a-z][a-z0-9]*>/i
According to the standard, HTML tag name may have a digit at any… Regexp “opening or closing HTML-tag without attributes”: /<\/?… For instance, for HTML tags we could use a simpler regexp: <\w+>.… But as HTML has stricter restrictions for a tag name, <[a-z][a-z0-9]*> is more reliable.
Once a custom element is defined, we can use it on par with built-in HTML elements.… That’s great, as HTML dictionary is rich, but not infinite.… That’s strange for an HTML element.… They are unknown to search engines, and accessibility devices can’t handle them.… HTML Living Standard: https://html.spec.whatwg.org/#custom-elements.
Shorter notation: form.name
There’s a shorter notation: we can access… That also works, but there’s a minor issue: if we access an element, and then change its name, then it… </textarea> holds its value as nested HTML, we should never use textarea.innerHTML to access it… It stores only the HTML that was initially on the page, not the current value.… The difference between defaultSelected and selected is that defaultSelected sets the HTML-attribute (
HTML attributes.In HTML, tags may have attributes.… Is there a way to access such attributes?
Sure.… These methods operate exactly with what’s written in HTML.… HTML-elements for JavaScript.… We want to read the value “as written” in HTML.
From it we can access any node.… That’s the DOM node of the <html> tag.… For instance, <head> and <body> are children of <html> element.… Properties firstChild and lastChild give fast access to the first and last children.… There are also additional navigation properties for HTML forms.
For instance, we need to find programming languages: HTML, PHP, Java or JavaScript.… The corresponding regexp: html|php|java(script)?.… CSS matches I love HTML or CSS.… I love (HTML|CSS) matches I love HTML or I love CSS.… First, the hours:
If the first digit is 0 or 1, then the next digit can be any: [01]\d.
They can be written right in a web page’s HTML and run automatically as the page loads.… The aim is to prevent an evil webpage from accessing private information or harming the user’s data.… It has no direct access to OS functions.… Modern browsers allow it to work with files, but the access is limited and only provided if the user… There are at least three great things about JavaScript:
Full integration with HTML/CSS.
It allows a component to have its very own “shadow” DOM tree, that can’t be accidentally accessed from… Shadow tree – a hidden DOM subtree, not reflected in HTML, hidden from prying eyes.… Any code is able to access the shadow tree of elem.… There’s no way to access them.… If mode="open", then it’s accessible as elem.shadowRoot property.
There’s also an autofocus HTML attribute that puts the focus onto an element by default when a page loads… This can be changed using HTML-attribute tabindex.… That is: if we have two elements, the first has tabindex="1", and the second has tabindex=&… That is, when we switch elements, elements with tabindex=0 go after elements with tabindex ≥ 1.… tabindex="-1" allows only programmatic focusing on an element.
For instance, server-side scripts that download HTML pages and process them can also use the DOM.… Specifications
The BOM is a part of the general HTML specification.… The HTML spec at https://html.spec.whatwg.org is not only about the “HTML language” (tags, attributes… That’s “HTML in broad terms”.… HTML specification
Describes the HTML language (e.g. tags) and also the BOM (browser object model) –
So that obj[1] is the same as obj["1"], and obj[true] is the same as obj["true"].… However, symbols cannot be accessed accidentally.… We can create symbols in it and access them later, and it guarantees that repeated accesses by the same… Also it won’t be accessed directly, because another script does not have our symbol.… There are many system symbols used by JavaScript which are accessible as Symbol.*.
Document events:
DOMContentLoaded – when the HTML is loaded and processed, DOM is fully built.… HTML-attribute.A handler can be set in HTML with an attribute named on<event>.… Accessing the element: this.The value of this inside a handler is the element.… The event object is also available in HTML handlers
If we assign a handler in HTML, we can… HTML attributes are used sparingly, because JavaScript in the middle of an HTML tag looks a little bit
Here the flow is:
The initial promise resolves in 1 second (*),
Then the .then handler is called (**… As the result is passed along the chain of handlers, we can see a sequence of alert calls: 1 → 2 → 4.… So in the code above all alert show the same: 1.… So the output is the same as in the previous example: 1 → 2 → 4, but now with 1 second delay between… In the example above resolve(2) is called after 1 second (**).
is a safe way to access nested object properties, even if an intermediate property doesn’t exist.… Here’s the safe way to access user.address.street using ?.… Further properties are accessed in a regular way.… [] syntax also works, if we’d like to use brackets [] to access properties instead of dot ..… allows to safely access nested properties.
Still, we should apply ?.
Some features are well-supported and integrated into the modern HTML/DOM standard, while others are yet… DOM structure, managed solely by its class, outside code doesn’t access it (“encapsulation” principle… Custom elements – to define custom HTML elements.
In this HTML, a click on a link doesn’t lead to navigation; the browser doesn’t do anything:… some CSS:
Menu items are implemented as HTML-links… We forever deny access to information about right-clicks for any outer code, including counters that… But we should generally keep the semantic meaning of HTML elements.… Besides being “just a good thing”, that makes your HTML better in terms of accessibility.
new Date(milliseconds)
Create a Date object with the time equal to number of milliseconds (1/… The date parameter is actually the day of month, if absent then 1 is assumed.… instance:
The maximal precision is 1… ms (1/1000 sec):… It may be “2 Mar” or “1 Mar” in case of a leap-year. We don’t need to think about it.
To grant JavaScript access to any other response header, the server must send the Access-Control-Expose-Headers… They can take it into account when checking access rights.… Access-Control-Request-Method – requested method.… : https://javascript.info
Access-Control-Allow-Methods: PATCH
Access-Control-Allow-Headers: Content-Type… list of allowed methods,
Access-Control-Allow-Headers with a list of allowed headers,
Access-Control-Max-Age
Different websites can’t access each other’s databases.… for the first time and have no database, and also for version 1, for upgrades.… Let’s say:
A visitor opened our site in a browser tab, with database version 1.… And it can’t be both version 1 and 2.… , at (1).
But what if we’d like to insert an HTML string “as html”, with all tags and stuff working, in the same… " – insert html into elem, at the beginning,
"beforeend" – insert html into elem, at the… The second parameter is an HTML string, that is inserted “as HTML”.… Given some HTML in html, elem.insertAdjacentHTML(where, html) inserts it depending on the value of where… ; – insert html right after elem.
The 2 gets concatenated to '1', so it’s like '1' + 2 = "12" and "12" + 2 = "… For example, if we are getting values from HTML form fields, they are usually strings.… For example, write (1 + 2) * 2.
There are many operators in JavaScript.… So, the alert shows 1.… It’s like (a = 1 + 2), 3 + 4.
Property values are accessible using the dot notation:… And then we use it to access the property. That gives us a great deal of flexibility.… But if we run the code, we see a totally different picture:
USA (1) goes first
then Switzerland (41)… So we see 1, 41, 44, 49.
Integer properties? What’s that?… To access a property, we can use:
The dot notation: obj.property.
Here’s the similar code that causes user to download the dynamically created Blob, without any HTML:… So such URLs are short, but allow to access the Blob.… In the previous example with the clickable HTML-link, we don’t call URL.revokeObjectURL(link.href), because… Direct access to blob, no “encoding/decoding”
Blob to data urlNo need to revoke anything
The x axis is the time: 0 – the start, 1 – the end of transition-duration.… That can be specified by the curve cubic-bezier(0, 0, 1, 1).… The linear is a shorthand for cubic-bezier(0, 0, 1, 1) – a straight line, which we described above.… The y is out of the “standard” range 0..1.… That is, the animation starts immediately and takes 1 step.
To access individual bytes, another “view” object is needed, not buffer[index].… To access the underlying ArrayBuffer, there are following properties in TypedArray:
buffer – references… It allows to access the data on any offset in any format.… With DataView we access the data with methods like .getUint8(i) or .getUint16(i).… We can access it as .buffer and make another view if needed.
We can also access cookies from the browser, using document.cookie property.… By default, a cookie is accessible only at the domain that set it.… It makes the cookie accessible for pages under that path. By default, it’s the current path.… This attribute forbids any JavaScript access to the cookie.… Summary.document.cookie provides access to cookies.
The lifecycle of an HTML page has three important events:
DOMContentLoaded – the browser fully loaded… HTML, and the DOM tree is built, but external resources like pictures <img> and stylesheets may… not yet have loaded.
load – not only HTML is loaded, but also all the external resources: images, styles… DOMContentLoaded and scripts.When the browser processes an HTML-document and comes across a <script… The typical output:
[1] initial readyState:loading
[2] readyState:interactive
[2] DOMContentLoaded
[
Sleep until a task appears, then go to 1.… Use-case 1: splitting CPU-hungry tasks.Let’s say we have a CPU-hungry task.… count does a part of the job (*), and then re-schedules itself (**) if needed:
First run counts: i=1.… queued and then executes when part 1 finished, before the next part.… Go to step 1.
To schedule a new macrotask:
Use zero delayed setTimeout(f).
This chapter is about sending HTML forms: with or without files, with additional fields and so on.… As you might have guessed, it’s the object to represent HTML form data.… The constructor is:
If HTML form element… Summary.FormData objects are used to capture HTML form and submit it using fetch or another network method… We can either create new FormData(form) from an HTML form, or create an object without a form at all,
str.search(regexp).The method str.search(regexp) returns the position of the first match or -1… part of the string before the match
$'
inserts a part of the string after the match
$n
if n is a 1-… If there are many groups, it’s convenient to use rest parameters to access… gives us the ultimate replacement power, because it gets all the information about the match, has access
Crossorigin policy.There’s a rule: scripts from one site can’t access contents of the other site.… There are three levels of cross-origin access:
No crossorigin attribute – access prohibited.
crossorigin… ="anonymous" – access allowed if the server responds with the header Access-Control-Allow-Origin… allowed if the server sends back the header Access-Control-Allow-Origin with our origin and Access-Control-Allow-Credentials… So the cross-origin access was prohibited. Let’s add it.
Using setInterval.An animation can be implemented as a sequence of frames – usually small changes to HTML… like CSS-property transition-timing-function that gets the fraction of time that passed (0 at start, 1… The value progress=0 denotes the beginning animation state, and progress=1 – the end state.… Gets a time fraction from 0 to 1, returns the animation progress, usually from 0 to 1.
draw – the function
We already know that a function can access variables outside of it (“outer” variables).… It can access the outer variables and so can return the full name.… No matter where, it still has access to the same outer variables.… Step 1.… In theory, it should be accessible, but the engine optimized it out.
The “script” tag.JavaScript programs can be inserted almost anywhere into an HTML document using the… used nowadays but can still be found in old code:
The type attribute: <script type=…>
The old HTML… Also, the modern HTML standard totally changed the meaning of this attribute.… Script files are attached to HTML with the src attribute:… Please note:
As a rule, only the simplest scripts are put into HTML
External interface – methods and properties, accessible also from outside the class.… those extending it (like private, but plus access from inheriting classes).… They are only accessible from inside the class.… We can’t access it from outside or from inheriting classes.… JavaScript makes sure we can only access those from inside the class.
The error occurs in the line (*) that tries to access… They both call func with arguments 1,… Append glue and this[1].
Append glue and this[2].
…Do so until this.length items are glued.… So, technically it takes this and joins this[0], this[1] …etc together.… There exists a way to create decorators that keep access to function properties, but this requires using
In the demo above they are labeled: 1, 2, 3.
Build segments between control points 1 → 2 → 3.… The formula for a 2-points curve:
P = (1-t)P1 + tP2
For 3 control points:
P = (1−t)2P1 + 2(1−t)tP2… + t2P3
For 4 control points:
P = (1−t)3P1 + 3(1−t)2tP2 +3(1−t)t2P3 + t3P4
These are vector equations… For instance, if control points are (0,0), (0.5, 1) and (1, 0), the equations become:
x = (1−t)2 *… 0 + 2(1−t)t * 0.5 + t2 * 1 = (1-t)t + t2 = t
y = (1−t)2 * 0 + 2(1−t)t * 1 + t2 * 0 = 2(1-t)t = –2t2 +
Only first 50 results are shown.