vef1-2025

Vefforritun 1 kennd haustið 2025

View the Project on GitHub vefforritun/vef1-2025

Fyrirlestur – DOM & vafrinn

Vefforritun 1 — TÖL107G

Ólafur Sverrir Kjartansson, osk@hi.is


HTML & JavaScript

Í æskilegri röð:

  1. <script src="scripts.js"></script>
  2. Innan <script>...</script>
  3. Forðast að binda JavaScript við element með on* attributes (t.d. onclick)

Að teikna síðu

HTML, CSS og JavaScript er ekki sótt á augabragði, í einfaldaðri mynd gerist:



Skilvirkar skriptur




DOM


<!doctype html>
<html>
  <head>
    <title>My home page</title>
  </head>
  <body>
    <h1>My home page</h1>
    <p>Hello, I am Marijn and this is my home page.</p>
    <p>I also wrote a book! Read it
      <a href="http://eloquentjavascript.net">here</a>.
    </p>
  </body>
</html>

HTML box


window


HTML



HTML tré


DOM staðall


Dæmi um óþægindi:


Ferðast um DOM

Hver nóða hefur vísanir í foreldri, börn og systkini:


Ferðast um DOM



NodeList


function walk(node) {
  const {
    childNodes, nodeType, nodeValue,
  } = node;

  if (nodeType === Node.ELEMENT_NODE) {
    for (const child of Array.from(childNodes)) {
      walk(child);
    }
  } else if (nodeType === Node.TEXT_NODE) {
    console.log(`Textanóða: ${nodeValue}`);
  }
}

walk(document.body);

Dæmi


Finna element


getElementById


getElementsByTagName


getElementsByClassName


querySelector


Leitað í trjám


Depth first

Leitað depth first


Breadth first

Leitað breadth first


querySelectorAll


Notum querySelector og querySelectorAll lang mest þegar við erum að leita að elementum.

Samlegðaráhrif á milli CSS og JavaScript gegnum selectora!


Dæmi


Breytingar á DOM tré


<p>One</p>
<p>Two</p>
<p>Three</p>
const paragraphs =
  document.body.getElementsByTagName('p');

document.body.insertBefore(
  paragraphs[2],
  paragraphs[0],
);

Að búa til element


const p = document.createElement('p');

p.setAttribute('class', 'hello');
const txt = document.createTextNode('Hæ!');
p.appendChild(txt); // bætum texta við p

// bætum p við, aftast í body
document.documentElement
  .lastChild.appendChild(p);

Attributes


const input =
  document.createElement('input');

input.setAttribute('type', 'text');
input.setAttribute('id', 'searchField');
input.setAttribute('required', true);
input.setAttribute('value', 'foo');

// gildi breytt í "foobar" í input
input.getAttribute('value'); // "foo"
input.value; // "foobar"

Dæmi um að færa til element

Dæmi um að búa til element

Dæmi um attribute


Hjálparföll


function el(name, ...children) {
  const e = document.createElement(name);

  for (const child of children) {
    if (typeof child === 'string') {
      e.appendChild(
        document.createTextNode(child),
      );
    } else {
      e.appendChild(child);
    }
  }

  return e;
}

document.body.append(
  el('div',
    el('p', 'Halló heimur!'),
    el('ul',
      el('li', 'einn'),
      el('li', 'tveir'),
      el('li', 'þrír'),
    ),
  ),
);

<div>
  <p>Halló heimur!</p>
  <ul>
    <li>einn</li>
    <li>tveir</li>
    <li>þrír</li>
  </ul>
</div>

Dæmi


Geyma gögn á elementum


Dæmi


Classlist



const div =
  document.createElement('div');

div.classList.add('text');
div.classList.add('important');
// class="text important"
div.classList.remove('text');
div.classList.toggle('important');
// class=""
div.classList.toggle('important');
// class="important"

JavaScript og CSS



const div = document.querySelector('div');

// hlutur með öllum stílum á div
div.style;

// setjum margin á div
div.style.margin = '10px 20px 30px 10px';

Dæmi


Uppfæra textanóður



Dæmi


Layout



Dæmi


Layout og repaint


Dæmi


Animation og JavaScript


requestAnimationFrame


Dæmi