JavaScript Essentials For Dummies (eBook)
241 Seiten
John Wiley & Sons (Verlag)
978-1-394-26322-6 (ISBN)
The concise and digestible get-started guide to JavaScript programming
JavaScript Essentials For Dummies is your quick reference to all the core concepts about JavaScript-the dynamic scripting language that is often the final step in creating powerful websites. This no-nonsense book gets right to the point, eliminating review material, wordy explanations, and fluff. Find out all you need to know about the foundations of JavaScript, swiftly and crystal clear. Perfect for a brush-up on the basics or as an everyday desk reference on the job, this is the reliable little book you can always turn to for answers.
- Get a quick and thorough intro to the basic concepts of coding with JavaScript
- Review what you've already learned or pick up essential new skills
- Add interactive features to websites with JavaScript programming
- Keep this concise reference book handy for jogging your memory as you work
This book is to the point, focusing on the key topics you need to know about this popular programming language. Great for supplementing classroom learning, reviewing for a certification, or staying knowledgeable on the job.
Paul McFedries is a longtime technical author with more than 100 published titles. His work includes Web Coding & Development All-in-One For Dummies, iPad and iPad Pro For Dummies, Alexa For Dummies, G Suite For Dummies, and Excel Data Analysis For Dummies.
Paul McFedries is a longtime technical author with more than 100 published titles. His work includes Web Coding & Development All-in-One For Dummies, iPad and iPad Pro For Dummies, Alexa For Dummies, G Suite For Dummies, and Excel Data Analysis For Dummies.
Chapter 1
JavaScript: The Big Picture
IN THIS CHAPTER
Getting a feel for programming in general, and JavaScript in particular
Checking out the tools you need to get coding
Adding comments to your JavaScript code
Storing your code in a separate JavaScript file
In this chapter, you explore some useful JavaScript basics. Don’t worry if you’ve never programmed before. I take you through everything you need to know, step-by-step, nice and easy. As you’re about to find out, it really is fun to program.
Adding JavaScript Code to a Web Page
Okay, it’s time to roll up your sleeves, crack your knuckles, and start coding. This section describes the standard procedure for constructing and testing a script and takes you through a couple of examples.
The <script> tag
The basic container for a script is, naturally enough, the HTML <script>
tag and its associated </script>
end tag:
<script>
JavaScript statements go here
</script>
Where do you put the <script> tag?
With certain exceptions, it doesn’t matter a great deal where you put your <script>
tag. Some people place the tag between the page’s </head>
and <body>
tags. The HTML standard recommends placing the <script>
tag within the page header (that is, between <head>
and </head>
), so that’s the style I use in this book:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Where do you put the script tag?</title>
<script>
JavaScript statements go here
</script>
</head>
<body>
</body>
</html>
Here are the exceptions to the put-your-script-anywhere technique:
- If your script is designed to write data to the page, the
<script>
tag must be positioned within the page body (that is, between the<body>
and</body>
tags) in the exact position where you want the text to appear. - If your script refers to an item on the page (such as a form object), the script must be placed after that item.
- With many HTML tags, you can add one or more JavaScript statements as attributes directly within the tag.
It’s perfectly acceptable to insert multiple <script>
tags within a single page, as long as each one has a corresponding </script>
end tag, and as long as you don’t put one <script>
block within another one.
Example #1: Displaying a message to the user
You’re now ready to construct and try out your first script. This example shows you the simplest of all JavaScript actions: displaying a basic message to the user. The following code shows the script within an HTML file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Displaying a Message to the User</title>
<script>
alert("Hello JavaScript World!");
</script>
</head>
<body>
</body>
</html>
As shown in here, place the script within the header of a page, save the file, and then open the HTML file within your browser.
This script consists of just a single line:
alert("Hello JavaScript World!");
This is called a statement, and each statement is designed to perform a single JavaScript task. Your scripts will range from simple programs with just a few statements to huge projects consisting of hundreds of statements.
You may be wondering about the semicolon (;
) that appears at the end of the statement. Good eye. You use the semicolon to mark the end of each of your JavaScript statements.
In the example, the statement runs the JavaScript alert()
method, which displays to the user whatever message is enclosed within the parentheses (which could be a welcome message, an announcement of new features on your site, an advertisement for a promotion, and so on). Figure 1-1 shows the message that appears when you open the file.
FIGURE 1-1: This “alert” message appears when you open the HTML file containing the example script.
How did the browser know to run the JavaScript statement? When a browser processes (parses, in the vernacular) a page, it basically starts at the beginning of the HTML file and works its way down, one line at a time. If it trips over a <script>
tag, it knows one or more JavaScript statements are coming, and it automatically executes those statements, in order, as soon as it reads them. The exception is when JavaScript statements are enclosed within a function, which I explain in Chapter 5.
One of the cardinal rules of JavaScript programming is “one statement, one line.” That is, each statement must appear on only a single line, and there should be no more than one statement on each line. I said “should” in the second part of the previous sentence because it is possible to put multiple statements on a single line, as long as you separate each statement with a semicolon (;
). There are rare times when it’s necessary to have two or more statements on one line, but you should avoid it for the bulk of your programming because multiple-statement lines are difficult to read and to troubleshoot.
Example #2: Writing text to the page
One of JavaScript’s most powerful features is the capability to write text and even HTML tags and CSS rules to the web page on-the-fly. That is, the text (or whatever) gets inserted into the page when a web browser loads the page. What good is that? For one thing, it’s ideal for time-sensitive data. For example, you may want to display the date and time that a web page was last modified so that visitors know how old (or new) the page is. Here’s some code that shows just such a script:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Writing Data to the Page</title>
</head>
<body>
This is a regular line of text.<br>
<script>
document.write("Last modified: " + document.lastModified)
</script>
<br>This is another line of regular text.
</body>
</html>
Notice how the script appears within the body of the HTML document, which is necessary whenever you want to write data to the page. Figure 1-2 shows the result.
FIGURE 1-2: When you open the file, the text displays the date and time the file was last modified.
This script makes use of the document
object, which is a built-in JavaScript construct that refers to whatever HTML file (document) the script resides in (check out Chapter 6 for more about the document
object). The document.write()
statement tells the browser to insert whatever is within the parentheses to the web page. The document.lastModified
portion returns the date and time the file was last changed and saved.
What You Need to Get Started
One of the nicest things about HTML and CSS is that the hurdles you have to leap to get started are not only short but few in number. In fact, you really need only two things, both of which are free: a text editor to enter the text, tags, and properties; and a browser to view the results. (You’ll also need a web server to host the finished pages, but the server isn’t necessary when you’re creating the pages.) Yes, there are high-end text editors and fancy graphics programs, but these fall into the “Bells and Whistles” category; you can create perfectly respectable web pages without them.
The basic requirements for JavaScript programming are exactly the same as for HTML: a text editor and a browser. Again, programs are available to help you write and test your scripts, but you don’t need them.
Dealing with Two Exceptional Cases
In this book, I make a couple of JavaScript assumptions related to the people who’ll be visiting the pages you post to the web:
- Those people have JavaScript enabled in their web browser.
- Those people are using a relatively up-to-date version of a modern web browser, such as Chrome, Edge, Safari, or Firefox.
These...
Erscheint lt. Verlag | 27.3.2024 |
---|---|
Sprache | englisch |
Themenwelt | Mathematik / Informatik ► Informatik ► Programmiersprachen / -werkzeuge |
Informatik ► Web / Internet ► JavaScript | |
Schlagworte | basic javascript programming • Computer Science • easy javascript • Informatik • JavaScript • javascript books • javascript coding • JavaScript language • javascript manual • JavaScript Programming • Javascript programming beginners • javascript quick • javascript quick reference • javascript quick review • javascript reference • javascript review • javascript web • Programmierung • Programmierung u. Software-Entwicklung • Programming & Software Development |
ISBN-10 | 1-394-26322-8 / 1394263228 |
ISBN-13 | 978-1-394-26322-6 / 9781394263226 |
Informationen gemäß Produktsicherheitsverordnung (GPSR) | |
Haben Sie eine Frage zum Produkt? |
Größe: 2,2 MB
Kopierschutz: Adobe-DRM
Adobe-DRM ist ein Kopierschutz, der das eBook vor Mißbrauch schützen soll. Dabei wird das eBook bereits beim Download auf Ihre persönliche Adobe-ID autorisiert. Lesen können Sie das eBook dann nur auf den Geräten, welche ebenfalls auf Ihre Adobe-ID registriert sind.
Details zum Adobe-DRM
Dateiformat: EPUB (Electronic Publication)
EPUB ist ein offener Standard für eBooks und eignet sich besonders zur Darstellung von Belletristik und Sachbüchern. Der Fließtext wird dynamisch an die Display- und Schriftgröße angepasst. Auch für mobile Lesegeräte ist EPUB daher gut geeignet.
Systemvoraussetzungen:
PC/Mac: Mit einem PC oder Mac können Sie dieses eBook lesen. Sie benötigen eine
eReader: Dieses eBook kann mit (fast) allen eBook-Readern gelesen werden. Mit dem amazon-Kindle ist es aber nicht kompatibel.
Smartphone/Tablet: Egal ob Apple oder Android, dieses eBook können Sie lesen. Sie benötigen eine
Geräteliste und zusätzliche Hinweise
Buying eBooks from abroad
For tax law reasons we can sell eBooks just within Germany and Switzerland. Regrettably we cannot fulfill eBook-orders from other countries.
aus dem Bereich