reading-notes


Project maintained by Razan-am Hosted on GitHub Pages — Theme by mattgraham

JAVASCRIPT

Introduction

  1. ACCESS CONTENT :You can use JavaScript to select any element, attribute, or text from an HTML page.
    • Select the text inside all of the 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:You can use JavaScript to add elements, attributes, and text to the page, or remove them.
    • Add a paragraph of text after the first element
    • Change the value of class attributes to trigger new CSS rules
  3. PROGRAM RULES:You can specify a set of steps for the browser to follow (like a recipe),which allows it to access or change the content of a page.
    • A gallery script could check which image a user clicked on and display a larger version of that image.
  4. REACT TO EVENTS:You can specify that a script should run when a specific event has occurrede, it could be run when:
    • A button is pressed
    • A link is clicked (or tapped) on
    • A cursor hovers over an element

JAVASCRIPT IN THE BROWSER

Before getting deep in the javascript this is a quiqk refresh for HTML & CSS:


Before you learn how to read and write the JavaScript language itself, you need to become familiar with some key concepts in computer programming and they are :

What is a script and how do I create one?

A browser may use different parts of the script depending on how the user interacts with the web page. Script can run different sections of the code in response to the situation around them.


WRITING A SCRIPT

Designing a script diagram:


FROM STEPS TO CODE

So To approach writing a script, break down your goal into a series of tasks and then work out each step needed to complete that task (a flowchart can help).


Basic javascript instructions:

1. EXPRESSIONS:

2. OPERATORS:

- Arithmetic operators :

Example for arithmetic operator: // Subtotal is 70 var subtotal (13 + 1) * 5; // Shipping is 7 var shipping 0.5 * (13 + 1) ; // Total is 77 var total subtotal + shipping ;

var el Sub document .getElementByid(‘ subtotal ‘) ; elSub .textContent =subtotal ;

var elShip = document .getElement Byid(‘shi ppi ng ‘) ; elShip.textContent =shipping;

var elTotal = document .getElementByid(‘total ‘); elTotal .textContent =total;


- Srting operator :

Example: var greeting= ‘Howdy ‘; var name= ‘Mol ly’ ; var welcomeMessage = greeting+ name+ ‘!’; var el = document.getElementByld(‘greeting’); el .textContent = welcomeMessage;


Function & methods

- Function and method :

  1. Functions :consist of a series of statements that have been grouped together because they perform a specific task.

    If different parts of a script repeat the same task, you can reuse the function.

Example: //variable var msg = ‘Sign up to receive our newsletter for 10% off!’; //function function updateMessage() { var el = document.getElementByld(‘message’}; el .textContent = msg; } //calling the function updateMessage(};


  1. Method :is the same as a function, except methods are created inside (and are part of) an object.

References:

@Jon Duckett/JAVASCRIPT