Skip to content

K128-Mersin-JavaScript-React-Bootcamp/1-week-sunday-lesson

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

  1. DOM Nedir?

    • Document Object Model
    1. scipt neden alta konur?
  2. innerHTML

<html>
<body>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = "Hello World!";
</script>

</body>
</html>
  1. getElementsByTagName("p")
  2. Chaining
const x = document.getElementById("main");
const y = x.getElementsByTagName("p");
  1. getElementsByClassName("intro")
  2. Finding elements by CSS selectors
const x = document.querySelectorAll("p.intro");
  1. Form elemanlarının alınması
<form action="/login" method="post" id="frm1">
    <input type="text" name="username" id="user" value="zafer">
    <input type="password" name="password" id="passw" value="1234">
</form>
<script>
const myForm = document.forms["frm1"];
let text = "";
for (let i = 0; i < myForm.length; i++) {
    text += myForm.elements[i].value;
}
console.log(text);
<script>
  1. FormData()
const myForm = document.forms["frm1"];
var object = {};
const formData = new FormData(myForm);
formData.forEach(function(value, key){
    object[key] = value;
});
var json = JSON.stringify(object);
  1. Change attribute
<img id="myImage" src="smiley.gif">

<script>
document.getElementById("myImage").src = "https://via.placeholder.com/140x100";
</script>
  1. document.write
<p>Bla bla bla</p>

<script>
document.write(Date());
</script>

<p>Bla bla bla</p>
<script>
window.addEventListener('DOMContentLoaded', (event) => {
    document.write(Date());
});
</script>
  1. form validation
<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        input {
            display: block;
            margin-bottom: 5px;
        }
        .error {
            color:red;
            display: none;
            margin-bottom: 10px;
        }
    </style>
</head>
<body>

    <form action="/login" method="post" id="frm1" onsubmit="return validateForm()">
        <input type="text" name="username">
        <span class="error">Please fill</span>
        <input type="password" name="password">
        <span class="error">Please fill</span>
        <input type="submit" value="Submit">
    </form>

    <script>
    function validateForm () {
        const myForm = document.forms["frm1"];
        let isSuccess = true;
        if(!myForm.username.value) {
            myForm.username.nextElementSibling.style.display = 'block';
            isSuccess = false;
        }
        if(!myForm.password.value) {
            myForm.password.nextElementSibling.style.display = 'block';
            isSuccess = false;
        }
        
        return isSuccess;
    }
    </script>

</body>
</html>
  1. Automatic form validation
    <form action="/login" method="post" id="frm1">
        <input type="text" name="username" required oninvalid="this.setCustomValidity('Enter User Name Here')">
        <span class="error">Please fill</span>
        <input type="password" name="password" required>
        <span class="error">Please fill</span>
        <input type="submit" value="Submit">
    </form>
  1. Animation
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        img {
            width: 100px;
            height: 100px;
            position: absolute;
        }
    </style>
</head>
<body>
    <img id="car" src="car.png" alt="car">

    <script>
        const car = document.getElementById("car");
        let left = 0;
        const timer = setInterval(() => {

            car.style.left = `${left}px`;
            left+=10;
            if(left + 100 >= window.innerWidth) {
                clearInterval(timer);
            }
        }, 10)
    </script>
</body>
</html>
  1. addEventListener
document.getElementById("myBtn").addEventListener("click", alert(new Date));
  1. Event bubbling
<style>
  body * {
    margin: 10px;
    border: 1px solid blue;
  }
</style>

<form onclick="alert('form')">FORM
  <div onclick="alert('div')">DIV
    <p onclick="alert('p')">P</p>
  </div>
</form>
  1. Add event handler to window
window.addEventListener("resize", function(){
    document.getElementsByTagName("body")[0].innerHTML = window.innerWidth;
});
  1. Create elements
const paragraph = document.createElement("p");
const node = document.createTextNode("This is new.");
para.appendChild(node);
const element = document.getElementById("div1");
element.appendChild(para);
  1. Remove elements
<div id="div1">
  <p id="p1">This is a paragraph.</p>
  <p id="p2">This is another paragraph.</p>
</div>

<script>
const parent = document.getElementById("div1");
const child = document.getElementById("p1");
parent.removeChild(child);
</script>
  1. History Api
<button onclick="goToHello()">GoToHello</button>
<script>
    function goToHello() {
        const state = { 'page_id': 1, 'user_id': 5 }
        const title = 'Hellöö'
        const url = 'hello.html'
        history.pushState(state, title, url)
    }
</script>
  1. Navigator API
  2. Cookies
        document.cookie = "username=John Doe; expires=Thu, 18 Dec 2021 12:00:00 UTC; path=/hello";
  1. localStorage, sessionStorage(tab), websql, indexedDB
localStorage.setItem("lastname", "Smith");
localStorage.getItem("lastname");
localStorage.removeItem("lastname");
  1. Web Worker
<div id="result"></div>

<script>
    let w;
    startWorker();
    
    function startWorker() {
      if(typeof(w) == "undefined") {
        w = new Worker("demo_workers.js");
      }
      w.onmessage = function(event) {
        document.getElementById("result").innerHTML = event.data;
      };
    }
    
    function stopWorker() { 
      w.terminate();
      w = undefined;
    }
</script>
  1. Service workers https://mdn.github.io/sw-test/ https://github.com/mdn/sw-test/blob/gh-pages/sw.js

  2. Callbacks

function sum(x, y, myCallback) {
    const result = x + y;
    myCallback(result)
}
sum(x, y, console.log);
  1. Promise API
    • pending
    • fulfilled
    • rejected
let myPromise = new Promise(function(myResolve, myReject) {
  setTimeout(function() { myResolve("Hello World"); }, 3000);
});

myPromise.then(function(value) {
  document.getElementById("demo").innerHTML = value;
});
  1. Fetch API
fetch("myFile.txt")
    .then(x => x.text())
    .then(x => document.write(x));
  1. Async await
async function myDisplay() {
  let myPromise = new Promise(function(myResolve, myReject) {
    setTimeout(function() { myResolve("Hello World"); }, 3000);
  });
  document.getElementById("demo").innerHTML = await myPromise;
}

myDisplay();
  1. Generators
    • Kitap metaforu
function* generator() {
  yield 1;
  yield 2;
  yield 3;
}

const gen = generator(); // "Generator { }"

console.log(gen.next().value); // 1
console.log(gen.next().value); // 2
console.log(gen.next().value); // 3
function* infinite() {
    let index = 0;

    while (true) {
        yield index++;
    }
}

const generator = infinite(); // "Generator { }"

console.log(generator.next().value); // 0
console.log(generator.next().value); // 1
console.log(generator.next().value); // 2
function * iterableObj() {
  yield 'This';
  yield 'is';
  yield 'iterable.'
}
for (const val of iterableObj()) {
  console.log(val);
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages