concept page in category javascript

This is an excerpt from Manning's book Data Wrangling with JavaScript.
Web scraping is tedious, error-prone, and tiresome work. Your web scraping script depends on the structure of the page being scraped: if that structure changes, then your script will be broken. This makes web scraping scripts inherently fragile. For these reasons web scraping as a data source should be considered a last resort; you should use a more reliable alternative when possible.
Listing 11.1b JavaScript file for the browser-based visualization (listing-11.1/app.js)
function renderChart (bindto, data, size) { #1 var chart = c3.generate({ bindto: bindto, size: size, data: { json: data, keys: { x: "Year", value: [ "AvgTemp" ] } }, transition: { duration: 0 #2 } }); }; $(function () { $.get("nyc-temperature.csv") #3 .then(function (response) { var parseOptions = { header: true, dynamicTyping: true }; var parsed = Papa.parse(response, parseOptions); #4 renderChart("#chart", parsed.data); #5 }) .catch(function (err) { console.error(err); }); }); #1 This helper function renders a chart to a particular element on the page. #2 Disables animated transitions when we’re capturing a static image #3 Gets CSV data file from the web server #4 Parses CSV data to JavaScript data #5 Uses C3 to render the chart

This is an excerpt from Manning's book Secrets of the JavaScript Ninja, Second Edition.
He learned some PHP so that he could dynamically generate web pages, which he usually sprinkled with JavaScript in order to achieve complex functionality such as form validation and even dynamic on-page clocks! Fast-forward a couple of years, and smartphones had become a thing, so anticipating a large new market opening up, Bob went ahead and learned Objective-C and Java to develop mobile apps that run on iOS and Android.
Does the browser always build the page exactly according to the given HTML?
Listing 12.6. Examining the style property
<style> #A div { font-size: 1.8em; border: 0 solid gold; } </style> <div style="color:#000;" title="Ninja power!"> #B 忍者パワー </div> <script> document.addEventListener("DOMContentLoaded", () => { const div = document.querySelector("div"); assert(div.style.color === 'rgb(0, 0, 0)' || #C div.style.color === '#000', 'color was recorded'); assert(div.style.fontSize === '1.8em', #D 'fontSize was recorded'); assert(div.style.borderWidth === '0', #E 'borderWidth was recorded'); div.style.borderWidth = "4px"; #F assert(div.style.borderWidth === '4px', #G 'borderWidth was replaced'); }); </script> #A - Declares an in-page style sheet that applies font size and border information #B - This test element should receive multiple styles from various places, including its own style attribute and the style sheet. #C - Tests that the inlined color style was recorded #D - Tests that the inherited font size style was recorded #E - Tests that the inherited border width style was recorded #F - Replaces the border width style #G - Tests the border width style change was recorded !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[]"} !@%STYLE%@!

This is an excerpt from Manning's book CORS in Action: Creating and consuming cross-origin APIs.
Suppose you’re building a web mashup to load photos from the New York Public Library’s (NYPL) Flickr page and display them on your own page. What would the code look like? You could start with an HTML page to display the photos, add JavaScript code to load the photos from the Flickr page, and display them on the page. Pretty straightforward, right?
Console— Logs errors and other useful messages, and enables developers to interactively execute JavaScript on a page.
It allows you to enter JavaScript commands that can be run against the page. The console is the first place to check when something on the page goes wrong. With that in mind, if you reload the sample client, the error from figure 7.3 should appear.

This is an excerpt from Manning's book Third-Party JavaScript.
When a publisher includes your third-party script on their web page, often you know little about the context in which it’s being placed. Your script might get included on pages that sport a variety of different doctypes, DOM layouts, and CSS rules, and ought to work correctly in all of them.
You have to consider that a publisher might include your script at the top of their page, in the <head> tag, or they might include it at the bottom of the <body>. Publishers might load your application inside an iframe, or on a page where the <head> tag is entirely absent; in HTML5, head sections are optional, and not all browsers automatically generate one internally. If your script makes assumptions about these core elements when querying or appending to the DOM, it could wind up in trouble.
If you’re developing an embedded widget, displaying proper styles also becomes a concern. Is the widget being placed on a web page with a light background or a dark background? Do you want your widget to inherit styles and “blend” into the publisher’s web design, or do you want your widget to look identical in every context? What happens if the publisher’s HTML is malformed, causing the page to render in quirks mode? Solving these problems requires more than well-written CSS. We’ll cover solutions to these issues in later chapters.
The simplest way to output HTML to the publisher’s page is by using document.write. This function takes a string as input and outputs it as HTML to the document stream. This means that the HTML is output at the point at which the browser encounters the call to document.write. It only works in conjunction with a standard (blocking) script include, because the browser needs to block further page processing to wait for your document.write call to finish outputting. If it didn’t wait, document.write would output to wherever the browser was processing the page at that moment, and the result could be a mislocated widget, malformed HTML, or even a completely empty document.[1]
1 Attempting to call document.write after the page has already loaded will actually empty the DOM, presenting the user with a blank page. Needless to say, it can be destructive if not wielded properly.
Loading CSS files dynamically is fairly simple. You just need to create a <link> DOM element, set that element’s href attribute to point to your target CSS file, and append the element to the page. CSS files always load asynchronously when injected dynamically via JavaScript, so you won’t run the risk of blocking other files:
function loadStylesheet(url) { var link = document.createElement('link'); link.rel = 'stylesheet'; link.type = 'text/css'; link.href = url; var entry = document.getElementsByTagName('script')[0]; entry.parentNode.insertBefore(link, entry); }Again, not very difficult, but there’s a catch. This code doesn’t know when the stylesheet file has finished loading. If you try to render elements on the page before their corresponding CSS rules are loaded, there could be a short period of time when those elements are unstyled. In the web development world, this is referred to as FOUC, or flash of unstyled content. It’s safe to say that FOUC is undesirable.
Iframes are a great way to defend your content from infringing parent styles and JavaScript code. But their greatest benefit is also their major downside. As-is, your iframe will inherit nothing from the parent page, including font family, font size, font color, and a suite of other styles. In your effort to make your widget look consistent everywhere by embedding it in an iframe, it might end up looking strangely out of place because it doesn’t inherit some of the page’s native styles (see figure 3.6).
Now, if you’re using an iframe that’s hosting external content, things get trickier, but it’s not too bad. One method is to pass the detected styles as part of the URL that loads your external document. This can be done by converting the styles hash to query string or hash fragment parameters. Then, from JavaScript code running on the external document, extract the parameters from the URL (using some of the parameter-passing techniques from section 2.5), and use the applyStyles function to inject them into the page. Figure 3.7 illustrates this process.

This is an excerpt from Manning's book Prototype and Scriptaculous in Action.
To incorporate the widget into a page, then, the designer needs to add a few lines of code in the header of the page:
The sample code can be found in the subfolder named chap7. Since no server-side resources are required for any of the sample code for this chapter, you can choose to set up a web server to serve the pages, or you can simply open the HTML files in your browser.
Open the page by choosing the Simple Example link from the Drag and Drop section of the control panel for this chapter, or display the dragndrop/simple-example.html file in your browser. The initial display will look like the window in figure 7.6.
One by one, and in any order, drag the photo images from the left column to the right column and drop them. Note that upon a drop, the image moves from one column to the other and retains the order in which it is dropped. Figure 7.7 shows the page with two photo thumbnails already moved and a third drag operation in progress.
In our simple example of listing 7.1, as well as when testing the revert option, you may have noted that you could move any of the draggable elements to any pixel position on the page. With the snap option, you can constrain the movement to specific grid positions, with a grid of any size.