Courses/Design & Technique-Essential Web Design/Q2/03

From Publication Station
Revision as of 09:45, 5 May 2018 by Andre (talk | contribs) (Replaced content with " {{:Courses/Design & Technique-Essential Web Design/Q2/02}} {{:jQuery}}")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Javascript (JS)

Resources

what is JS?

JS is a simple programming language that runs inside webpages when the pages are loaded in the users' browser.

In the complex organism that can be a web-page, you have:

  • HTML - content & structure
  • CSS - look
  • Javascript - (artificial) intelligence.

You can use JS to

  • change HTML content
  • change element attributes
  • change an element style
  • retrieve the window size
  • make a timer
  • validate data

JS in a HTML page

Javascript appears inside the html page inside the <script> tag

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
  </head>
  <body>
    <h1>starting javascript-ing</h1>
    <p id='greetings' >within the script tag</p>

    <script>
   
    alert('Hello Javascript!');
                       
    </script>

  </body>
</html>

JS in a separate file

The JS is imported from another file.

HTML file:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    
    <script src="hello.js"></script>

  </head>
  <body>
    <h1>starting javascript-ing</h1>
    <p>within the script tag</p>
  </body>
</html>

JavaScript file: hello.js

alert("hello world! Greetings from javascript");

making statements

JS statements are like phrases, that do very specific tasks.

Each statement has to be terminated with ";"

Like the above alert('Welcome to this page');

the browser console

JS-console.png

Essential to prototype, and debug your JS code.




What is jQuery?

jQuery is a library for JavaScript, which is a programming language used to make websites interactive. It's like a set of handy tools that makes it easier for developers to work with JavaScript, allowing them to do things like adding animations and making web pages respond to user actions without writing as much code.

Getting started

1. Prepare your HTML file

Open Visual Studio Code and create a new file. Save the file as index.html, inside of a folder that will contain all the files for this website and noting else. In this file, enter the ! symbol and chose the first option that shows up underneath. This should create a default html file, ready to work with.

2. Add a JavaScript page

Open en new file and save it in the same folder as your html file, name it script.js. In de head of your html document, you should add a link to this JS file:

<script src="script.js"></script>

3. Add jQuery

Before you can use jQuery code in your JavaScript file, you need to load the jQuery library file. the easiest way to do that is to copy the following line of code into the head of your document, above the link to your script file.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>

4. Test if it works

Enter the code below in your script.js file to check whether everything works correctly:

$(function() {

alert('Hello world!');

});

This should wait for the page to be loaded and then show a message saying Hello world!