Teaching yourself JavaScript, are you? That's a good call, and what I've been doing as well. This article has my notes on the subject matter.
What JavaScript Does
This section helps me understand how JavaScript accesses and modifies content and markup in a web page being viewed. In a bulleted list, JavaScript allows you to:
1. ACCESS CONTENT
Select elements, attributes, or text from an HTML page. For example:
- Select the text inside all of the "h1" elements on a page
- Select any elements that have a class attribute with a value of note
- Find out what was entered into a text input whose id attribute has a value of email
2. MODIFY CONTENT
JavaScript allows you to add elements, attributes, and text to the page, or remove them. For example:
- Add a paragraph of text after the "h1" element
- Change the value of class attributes to trigger new CSS rules for those elements
- Change the size or position of an "img" element
3. TRIGGER A SERIES OF EVENTS FOR THE BROWSER TO FOLLOW
JavaScript allows you to specify a set of steps for the browser to follw (like a recipe), which allows it to access or change the content of a page. For example:
- A gallery script could check which image a user clicked on an ddisplay a larger version of that image.
- A mortgage calculator could collect values from a form, perform a calculation, and display repayments.
- An animation could check the dimensions of the browser window and move an image to the bottom of the viewable area (also known as the viewport).
4. REACT TO EVENTS
JavaScript allows you to specify a script should run when a spefici event has occurred. For example:
- A button is pressed
- A link is clicked or tapped on
- A cursor hovers over an element
- Information is added to a form
- An interval of time has passed
- A web page has finished loading
Glossary
This section helps me learn JavaScript keywords and terminology.
- Variables: Used to store numbers or text, but can also store complex data structures;
- Arrays: data structures that hold multiple variables
- Strings
- currentIndex:
- Type: variable
- Purpose: holds the index number of an object (when the page loads, this is 0)
- move():
- Type: function
- Purpose: moves from one object to the next
- advance()
- Type: function
- setTimeout()
- Type: method
- Syntax: var timeout = setTimeout(function, delay);
- Purpose: assigned to a variable, it executes a function after a number of milliseconds
- clearTimeout()
- click
- Type: event
Variables
Ex: var students = 25;
GLOBAL VS. LOCAL VARIABLES
- Global variables have the entire script as their scope, as well as and other scripts in the same HTM document. You define it outside any function, and using the var keyword is optional.
- Local variables have a single use as their scope. They can be used only within the function where they are created, and using the var keyword is required. Additionally, the variables in the function's parameter list are always local variables.
Note: If you use the var keyword every time, you'll usually end up with the right type of variable!
NAMING VARIABLES
- Each variable has a unique name of my choosing;
- Variable names:
- require the first character to be a letter or underscore;
- are case sensitive;
- must fit on one line (less than 25 characters).
- Variable names can include:
- letters;
- digits 0-9;
- upper and lowercase;
- underscores;
- Variables cannot include:
- spaces;
- punctutation
ASSIGNING VALUES
- Use the equal sign to assign a valua to a variable. For example: var lines = 40
- Use any expression to the right of the qual sign, including other variables. For example: lines = lines + 1
JS Syntax Shorthands
INCREMENTING & DECREMENTING VARIABLES
- Incrementing and decrementing variable syntax shorthands:
- The + = operator. For example: lines += 1 or lines ++;
- The - = operator. For example: lines -= 1 or lines --;
- You can alternatively use the ++ or -- operator:
- If the operator is after the variable name, the increment or decrement happens after the current expression is evaluated. For example: alert(lines++); displays an alert with the value 40, and then increments lines to 41.
- If the operator is before the variable name, the increment or decrement happens first and then the expression is evaluated. For example: alert(++lines); increments lines to 41 and then displays an alert with the value of 41.
Responsive Slider
A slider positions a series of items next to each other, but only shows one at a time. The image then slide from one to the next.
SLIDER SPECIFICS
This slider:
- Loads several panels, but only sows one at a time.
- Provides buttons that allow users to navigate between each of the slides
- A timer to move them automatically after a set of interval.
MECHANICS
This slider is programmed with the following CSS steps:
- When the page first loads, CSS hides all of the slides.
- CSS then sets the display property of the first slide block to make it visible.
The script then goes through each slide and:
- Assigns an index number to that slide
- Adds a button for it under the slide group. To keep track of which slide is currently being shown, the script uses a vaiable called "currentIndex" (holding the index number of the current slide). When the page loads, this is 0, so it shows the first slide. It also needs to know which slide it is moving to, which is stored in a variable called "newSlide":
- If the index number of the new slide is higher than the index number of the current slide, then the new slide is placed to the right of the group. As the visible slide is animated to the left, the new slide automatically starts to come into view, taking its place.
- If the index number of the new slide is lower than the current index, then the new slide is placed to the left of the group.
- After the animation, the hidden slides are placed behind the one that is currently active.
SCRIPT OVERVIEW
A jQuery selector finds the sliders within the HTML markup. An anonymous function then runs for each one to create the slide. There are four key parts to the function:
Setup
Each slider needs some variables, they are in function-level scope so they:
- Can have different values for each slider
- Do not conflict with variables outside the script
Changing Slide: move()
move() is used to move from one slide to another, and to update the buttons that indicate which slide is currently being shown. It is called when the user clicks on a button, and by the advance() function.
A timer to show the next slide after 4 seconds: advance()
A time will call move() after 4 seconds. To create a timer, JavScript's window object has a setTimeout() method. It executes a function after a number of milliseconds. The timer is often assigned to a variable, and it uses the following syntax: var timeout = setTimeout(function, delay);
- timeout is a variable name that will be used to identify the timer.
- function can be a named function or an anynymous function.
- delay is the number of milliseconds before the function should run
To stop the timer, call "clearTimeout()". It takes one parameter: the variable used to identify the timer: "clearTimeout(timeout);"
Processing each of the slides that appear within a slider
The code loops through each of the slides to:
- Create the slider
- Add a button for each slide with an event handler that calls the move() function when users click it
SLIDER SCRIPT
STEPS
- There may be several sliders on a page, So the script starts by looking for every element whose class attribute has a value of s1ider. For each one, an anonymous function is run to process that slider.
- Variables are created to hold:
- $this: The current slider
- $group: The element that wraps around the slides
- $slides: AIl of the slides in this slider
- $button: An array of buttons (one for each slide)
- currentIndex: The current slide
- timeout: The timer
- The move() function appears next.
- The advance () function creates the timer.
- It starts by clearing the current timer. A new timer is set and when the time has elapsed it will run an anonymous function.
- An if statement checks whether or not the current slide is the last one:
- If it is not the last slide then it calls move() with parameter that tells it to go to the next slide.
- Otherwise it tells move() to go to the first slide.
- Each slide is processed by an anonymous function.
- A <button> element is created for each slide.
- If the index number of that slide is the same as the number held in the currentIndex variable, then a class of active is added to that button.
- An event handler is added to each button. When clicked it calls the move () function. The slide's index number indicates which slide to move to.
- The buttons are then added to the button container, and to the array of buttons: This array is used by the move () function to indicate which slide is currently being shown:
- advance () is called to start the timer.
An anynymous function creates the button for each slide.
Logic:
- Creates the button
- Asks if the button represents the current slide:
- If yes, the button is classified as ".active"
- If no, a "click" event on a radio element:
- adds button to container & array
- calls move()
An advance() function to clear and reset the timer.
Logic:
- Calls clearTimeout() & setTimeout().
- Asks if the button represents the last slide:
- If yes, the function calls move() to first slide
- If no, the function calls move() to next slide
What jQuery Does
This section is dedicated to jQuery, which makes the process of writing scripts faster and easier.
Organize Me Later
This section is where I bullet out notes from my reading, to organized at a later time.
- Ajax is a set of techniques that allow you to just change part of a web page without reloading the entire page.
- Application Programming Interfaces (APIs) are part of of HTML5 and used by sites like Google Maps.
- When using jQuery, developers usually prefer to use a variable name with a dollar sign. It’s a personal practice to identify that it contains a jQuery object.