Introduction Flashcards
Programs in JS are called what ?
Scripts
Can we write JS scripts directly into the html ?
They can be written right in a web page’s HTML and run automatically as the page loads.
Do scripts need compilation before running ?
Scripts are provided and executed as plain text. They don’t need special preparation or compilation to run.
What we need to run a JS program ?
Today, JavaScript can execute not only in the browser, but also on the server, or actually on any device that has a special program called the JavaScript engine.
The browser has an embedded engine sometimes called ?
“JavaScript virtual machine”.
Examples of some JS engines in browsers today ?
V8 – in Chrome, Opera and Edge.
SpiderMonkey – in Firefox.
…There are other codenames like “Chakra” for IE, “JavaScriptCore”, “Nitro” and “SquirrelFish” for Safari, etc.
Briefly explain how the JS engine works ?
Engines are complicated. But the basics are easy.
The engine (embedded if it’s a browser) reads (“parses”) the script.
Then it converts (“compiles”) the script to the machine language.
And then the machine code runs, pretty fast.
The engine applies optimizations at each step of the process. It even watches the compiled script as it runs, analyzes the data that flows through it, and further optimizes the machine code based on that knowledge.
Do JS provides low-level access to memory to CPU ?
Modern JavaScript is a “safe” programming language. It does not provide low-level access to memory or CPU, because it was initially created for browsers which do not require it.
can we do webpage manipulation, interaction with the user and webserver with in-browser javascript ?
Yes
How to add new HTML to the page, change the existing content, modify styles.
we can do it with in browser js
Which type of js can React to user actions, run on mouse clicks, pointer movements, key presses.
In-browser JS
Which JS can Send requests over the network to remote servers, download and upload files (so-called AJAX and COMET technologies).
IN browser JS
Which JS can Get and set cookies, ask questions to the visitor, show messages.
In browser JS
Which JS can Remember the data on the client-side (“local storage”).
In browser JS
can In browser JavaScript on a webpage read/write arbitrary files on the hard disk
JavaScript on a webpage may not read/write arbitrary files on the hard disk, copy them or execute programs. 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 does certain actions, like “dropping” a file into a browser window or selecting it via an tag.
There are ways to interact with camera/microphone and other devices, but they require a user’s explicit permission. So a JavaScript-enabled page may not sneakily enable a web-camera, observe the surroundings and send the information to the NSA.
What is same origin policy ?
Different tabs/windows generally do not know about each other. Sometimes they do, for example when one window uses JavaScript to open the other one. But even in this case, JavaScript from one page may not access the other if they come from different sites (from a different domain, protocol or port).
This is called the “Same Origin Policy”. To work around that, both pages must agree for data exchange and contain a special JavaScript code that handles it. We’ll cover that in the tutorial.
This limitation is, again, for the user’s safety. A page from http://anysite.com which a user has opened must not be able to access another browser tab with the URL http://gmail.com and steal information from there.
can two different sites(having different servers) communicate with each other using JS
JavaScript can easily communicate over the net to the server where the current page came from. But its ability to receive data from other sites/domains is crippled. Though possible, it requires explicit agreement (expressed in HTTP headers) from the remote side. Once again, that’s a safety limitation.
Such limits do not exist if JavaScript is used outside of the browser, for example on a server. Modern browsers also allow plugin/extensions which may ask for extended permissions.