JavaScript Scratches

Hic sunt dracones

It's been over 10 years since I had to do anything involving JavaScript, so I thought it was about time to do some updating.

Well, surprise, surprise - it sure has changed!

After purchasing 'JavaScript. The Definitive Guide 7E' by David Flanagan, it was apparent to me I had some serious cogitation to do.

Most of the stuff I do is in a web environment. I run Apache web server on a Mac M1 desktop and maintain several web-hosted 'projects', so JavaScript knowledge is a must-have.

In case you hadn't noticed, or are unaware, the web now-a-daze is run mostly on JavaScript. Use your browser's 'View Source Code' feature on almost any web site and you will see that most (sometimes ALL) of the content is provided using JavaScript.

JavaScript was invented by Brendan Eich in 1995.

Installation

If you are reading this, you already have JavaScript available. It's part of your web browser, so does NOT have to be installed, unlike other programming languages like Ruby, Perl, Python, and C.

Since JavaScript is designed for a web environment, it makes sense to include it in the application designed for the web.

Your web browser also understands HTML and CSS inherently.

All you need to start programming in JavaScript is a text editor or IDE (Integrated Development Environment). I write my own code (hand-rolled) in a text editor BBEdit, so what you will see here is my way of writing code.

I also have Apache web server running locally, but that is not absolutely necessary. There are some things that will not work if you don't have a web server locally installed, but most of what you will see on these pages should.

One of the differences is the way you access a web page locally without a web server.

When viewing your local web pages in a browser WITHOUT a local web server, you have to use a different protocol instead of 'http://'.

If your web files are in a directory you would access them like this:

file:///Users/user/web/
NOTE: file:///

That goes in the address bar of your browser.

Another difference is that AJAX (Asynchronous JavaScript And XML) may not work as you expect. That is a security feature and a whole other rabbit hole not covered here.

Functions

Before you proceed there are 2 JavaScript functions I wrote that might assist you in your programming. You will see them in the source code when you view it.

Sprint is a function to print 'document.write', which is how JavaScript prints something to a web page.
I'm lazy so typing that a few thousand times gets old quickly. Use it like this:

Sprint('<h3>Hello world</h3>');

$Spacer is a function which prints a character x number of times to the screen. It prints the character "=" however many times you tell it. Thus:

$Spacer(30);
==============================

$Spacer can obviously be edited to print a different character (or even words), and how many times it prints.

Here's the code for both functions:

let Sprint = function(s) {
	document.write(s);
}
 $Spacer = function(s) {
	Sprint("=".repeat(s));
}