Search results

The callback gets one argument – the time passed from the beginning of the page… The code below shows the time between first 10 runs for requestAnimationFrame.… Usually it’s 10-20ms:… When a page is in the background, there are no repaints at all, so the callback won’t run: the animation
For instance, we should not set elem.style.top to 10, but rather to 10px.… But getComputedStyle does not give access to that color, because otherwise an arbitrary page could find… out whether the user visited a link by creating it on the page and checking the styles.… That’s to guarantee that there’s no side way for an evil page to test if a link was visited and hence
The transform will be split into 9 parts (10% each).… The process is progressing like this: 0s – -10% (first change in the beginning of the 1st second, immediately… process for steps(9, end) would go like this: 0s – 0 (during the first second nothing changes) 1s – -10%… The calculations may take time, especially on pages with many elements and a complex layout.
The lifecycle of an HTML page has three important events: DOMContentLoaded – the browser fully loaded… Let’s say we gather data about how the page is used: mouse clicks, scrolls, viewed page areas, and so… The transition to another page is not delayed: the browser leaves the page, but still performs sendBeacon… If we want to cancel the transition to another page, we can’t do it here.… The load event on window triggers when the page and all resources are loaded.
So touch devices worked well with web pages. But touch devices have more capabilities than a mouse.… Unless you develop for old browsers, such as Internet Explorer 10, or for Safari 12 or below, there’s
For example, once every 10 seconds.… That works, but there are downsides: Messages are passed with a delay up to 10 seconds (between requests… Even if there are no messages, the server is bombed with requests every 10 seconds, even if the user
The “clickjacking” attack allows an evil page to click on a “victim site” on behalf of the visitor.… Here’s how clickjacking was done with Facebook: A visitor is lured to the evil page.… The demo.Here’s how the evil page looks.… The header may have 3 values: DENY Never ever show the page inside a frame.… A hacker can post a link to their evil page in a message, or lure visitors to their page by some other
If there’s a bulky script at the top of the page, it “blocks the page”.… For instance, we can put a script at the bottom of the page.… Dynamic scripts.There’s one more important way of adding a script to the page.… So the user can read page content and get acquainted with the page immediately.… will see the page before the script loads.
How do we scroll the page using JavaScript?… The method scrollBy(x,y) scrolls the page relative to its current position.… For instance, scrollBy(0,10) scrolls the page 10px down.… If top=false, then the page scrolls to make elem appear at the bottom.… The page will “freeze” at its current scroll position.
By default, it’s 10.… It is one divided by ten 1/10, one-tenth.… So, division by powers 10 is guaranteed to work well in the decimal system, but division by 3 is not.… same reason, in the binary numeral system, the division by powers of 2 is guaranteed to work, but 1/10
JavaScript was initially created to “make web pages alive”.… They can be written right in a web page’s HTML and run automatically as the page loads.… For instance, in-browser JavaScript is able to: Add new HTML to the page, change the existing content… But even in this case, JavaScript from one page may not access the other page if they come from different… JavaScript can easily communicate over the net to the server where the current page came from.
DOM modification is the key to creating “live” pages.… Here we’ll see how to create new elements “on the fly” and modify the existing page content.… We’ll add a message on the page that looks nicer than alert.… It writes directly into the page text, while the DOM is not yet built.… To append HTML to the page before it has finished loading: document.write(html) After the page is loaded
When the page is scrolled to the very beginning, so that the top/left corner of the window is exactly… For instance click this button to see its window coordinates: If you scroll the page and repeat, you… But note the important detail: when the page is scrolled, the message flows away from the button.… message element relies on position:fixed, so it remains at the same place of the window while the page… Relative to the document – elem.getBoundingClientRect() plus the current page scroll.
What’s interesting about them is that the data survives a page refresh (for sessionStorage) and even… …And close/open the browser or just open the same page… Another tab with the same page will have a different storage.… The data survives page refresh, but not closing/opening the tab. Let’s see that in action.… You might want to open this page in two browser windows to test the code below.
The scroll event allows reacting to a page or element scrolling.… Load more data when the user scrolls down till the end of the page.
Web browsers give a means to control web pages. Node.js provides server-side features, and so on.… DOM (Document Object Model).The Document Object Model, or DOM for short, represents all page content… The document object is the main “entry point” to the page.… We can change or create anything on the page using it.… For instance, server-side scripts that download HTML pages and process them can also use the DOM.
Usually that header is set automatically and contains the url of the page that made the request.… "origin" – only send the origin in Referer, not the full page URL, e.g. only http://site.com… If we send a fetch, then by default it always sends the Referer header with the full url of our page… For example, we gather statistics on how the current visitor uses our page (mouse clicks, page fragments… When the visitor leaves our page – we’d like to save the data to our server.
One can also provide a relative path from the current page.… Other pages that reference the same script will take it from the cache instead of downloading it, so… That reduces traffic and makes pages faster.… We can use a <script> tag to add JavaScript code to a page.
”, and provides a URL to the next page.… It responds with a JSON of 30 commits, and also provides a link to the next page in the Link header.… The initial URL is https://api.github.com/repos/<repo>/commits, and the next page will be in the… We should get the next page URL from the Link header of the response.… The next page URL may look like https://api.github.com/repositories/93253246/commits?page=2.
We can split user interface into visual components: each of them has own place on the page, can “do”… separate visual entity that we can describe in terms of what it does and how it interacts with the page… In the case above, the page has blocks, each of them plays its own role, it’s logical to make these components
Open the example page in Chrome. Turn on developer tools with F12 (Mac: Cmd+Opt+I).… Navigator pane lists HTML, JavaScript, CSS and other files, including images that are attached to the page… Breakpoints.Let’s examine what’s going on within the code of the example page… the debugger (after we’ve set the breakpoints) is to reload the page.… So if our script dies with an error, we can open debugger, enable this option and reload the page to
It makes the cookie accessible for pages under that path. By default, it’s the current path.… Usually, we should set path to the root: path=/ to make the cookie accessible from all website pages.… can’t generate or extract from a remote page.… and waits for a user to visit that page.… Normally, if such a thing happens, and a user visits a web-page with a hacker’s JavaScript code, then
coordinates pageX/Y are counted from the left-upper corner of the document, and do not change when the page… When the page is scrolled, they change.… 500x500, and the mouse is in the left-upper corner, then clientX and clientY are 0, no matter how the page… Preventing copying If we want to disable selection to protect our page content from copy-pasting… Surely the user has access to HTML-source of the page, and can take the content from there, but not everyone
browser several details about it: how to show it, what to do when the element is added or removed to page… method connectedCallback() – the browser calls it when <time-formatted> element is added to page… Not just appended to another element as a child, but actually becomes a part of the page.… They will only be actually rendered when they make it into the page.… We call it once when the element is inserted into page.
The word “modal” means that the visitor can’t interact with the rest of the page, press other buttons… are modal: they pause script execution and don’t allow the visitor to interact with the rest of the page
for user.sayNow call it’s user) Then gives it ...argsBound – arguments from the partial call ("10
third-party script adds something into our document, and we’d like to detect, when it happens, to adapt our page… colored syntax highlighting into those elements, similar to what you see in examples here, on this page… Well, we can do it on DOMContentLoaded event, or put the script at the bottom of the page.… We can use MutationObserver to automatically detect when code snippets are inserted into the page and
That’s a formalization of what we see when browsing a page.… So after some time, it raises an alert like “Page Unresponsive”, suggesting killing the task with the… whole page.… For example, syntax-highlighting (used to colorize code examples on this page) is quite CPU-heavy.… s clearly noticeable, and if you are running it in-browser, then try to click other buttons on the page
Modules work only via HTTP(s), not locally If you try to open a web-page locally, via file… This is the correct variant: Resulthello.jsuser.jsindex.htmlIn the browser, if we talk about HTML pages… Here are two scripts on the same page, both type="module".… When using modules, we should be aware that the HTML page shows up as it loads, and JavaScript modules… run after that, so the user may see the page before the JavaScript application is ready.
A bad page could open tons of popup windows with ads.… Although, in practice they are severely limited, because in the past evil pages abused them.… to introduce many limitations to forbid the code like that and protect the user from ads and evils pages… That allows us to suspend/resume in-page activities, animations etc.… Summary.Popup windows are used rarely, as there are alternatives: loading and displaying information in-page
When the browser loads the page, it “reads” (another word: “parses”) the HTML and generates DOM objects… For instance, if the tag is <body id="page">, then the DOM object has body.id="page
All these objects are accessible using JavaScript, and we can use them to modify the page.… To do so, open the web page elk.html, turn on the browser developer tools and switch to the Elements… This works great when we have a huge HTML page (and corresponding huge DOM) and would like to see the… nodes have properties and methods that allow us to travel between them, modify them, move around the page
Image to blob.We can create a Blob of an image, an image part, or even make a page screenshot.… callbacks: For screenshotting a page… What it does is just walks the page and draws it on <canvas>.
result the first call 0 1 1 the second call 1 2 3 the third call 3 3 6 the fourth call 6 4 10… the fifth call 10 5 15 Here we can clearly see how the result of the previous call becomes the
Please remember them (or this page) for the cases when you need in-depth information about a particular
outputting goods from a list one after another or just running the same code for each number from 1 to 10
5 seconds asking for data, but in case the server is overloaded, it should increase the interval to 10
It was a toy language to decorate a web page. But web developers demanded more power.… People submitted it into <iframe>, just to stay on the current page, like this:… precise, there were actually tricks for that, they required special scripts at both the iframe and the page… For instance, if we request https://anywhere.com/request from https://javascript.info/page, the headers
Google Chrome.Open the page bug.html. There’s an error in the JavaScript code on it.
If you click the element below, the code elem.scrollTop += 10 executes.
A good page to see the current state of support for language features is https://compat-table.github.io
A user may switch between various applications, copy/paste different things, and a browser page shouldn
They do not affect the page. Summary.File objects inherit from Blob.
Let’s imagine a scenario where some third-party code or library interacts with elements on our page as… JavaScript, the “main” program – is our JavaScript-code, that runs and executes in our application or web page… images we need and create a collage, by clicking the "Create collage" button on the right side of the page… To increase page loading speed, it would be reasonable to download and display photo thumbnails in compressed
An exotic variable as a loop counter is especially cool if the loop body takes 1-2 pages (make it longer… For instance, the function printPage(page) will use a printer.
The page is scrolled (PageDown key). The browser opens the “Save Page” dialog (Ctrl+S) …and so on.
Origin – the origin of the client page, e.g. https://javascript.info.… codes like: 1001 – the party is going away, e.g. server is shutting down, or a browser leaves the page
The idea is that if a user has two pages open: one from john-smith.com, and another one is gmail.com,… Again, that’s only possible for pages with the same second-level domain.
In such case a web-browser suggests to kill the script and reload the page.… After some time it will suggest to reload the page.
The for..in loop is optimized for generic objects, not arrays, and thus is 10-100 times slower.
Only first 50 results are shown.