Skip to content

Instantly share code, notes, and snippets.

@yogain123
Created October 5, 2019 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yogain123/9e30a0be7b7c7520e0866000e11db5be to your computer and use it in GitHub Desktop.
Save yogain123/9e30a0be7b7c7520e0866000e11db5be to your computer and use it in GitHub Desktop.
Web Bootcamp
@yogain123
Copy link
Author

Browser environment, specs

Screen Shot 1941-05-02 at 11 19 18 PM



DOM

Screen Shot 1941-05-02 at 11 19 43 PM



BOM (Browser Object Model)

Screen Shot 1941-05-02 at 11 19 47 PM



Screen Shot 1941-05-02 at 11 20 05 PM

@yogain123
Copy link
Author

DOM Tree

The backbone of an HTML document are tags.

According to Document Object Model (DOM), every HTML-tag is an object. Nested tags are called “children” of the enclosing one.

The text inside a tag it is an object as well.

All these objects are accessible using JavaScript.

Screen Shot 1941-05-02 at 11 24 58 PM

@yogain123
Copy link
Author

Walking the DOM

Screen Shot 1941-05-02 at 11 25 48 PM

Screen Shot 1941-05-02 at 11 26 19 PM
Screen Shot 1941-05-02 at 11 27 10 PM
Screen Shot 1941-05-02 at 11 28 20 PM
Screen Shot 1941-05-02 at 11 28 28 PM
Screen Shot 1941-05-02 at 11 28 39 PM
Screen Shot 1941-05-02 at 11 29 41 PM

@yogain123
Copy link
Author

Searching element

Screen Shot 1941-05-02 at 11 34 22 PM
Screen Shot 1941-05-02 at 11 34 29 PM
Screen Shot 1941-05-02 at 11 35 26 PM
Screen Shot 1941-05-02 at 11 36 42 PM

@yogain123
Copy link
Author

inner and outer html

Screen Shot 1941-05-02 at 11 38 50 PM
Screen Shot 1941-05-02 at 11 39 02 PM



$_ will store the last printed value of console

Screen Shot 1941-05-02 at 11 43 14 PM

@yogain123
Copy link
Author

Event Listener

Screen Shot 1941-05-02 at 11 45 29 PM
Screen Shot 1941-05-02 at 11 45 50 PM
Screen Shot 1941-05-02 at 11 46 03 PM
Screen Shot 1941-05-02 at 11 46 29 PM

@yogain123
Copy link
Author

Event Delegation

Instead of putting event handler to all child , we delegate that task and put one event handler to its parent and , and in callback we get its child events and work using it ...

Screen Shot 1941-05-03 at 1 00 25 AM
Screen Shot 1941-05-03 at 1 01 19 AM
Screen Shot 1941-05-03 at 1 01 05 AM

@yogain123
Copy link
Author

Walking The DOM

screen shot 1939-11-27 at 4 32 20 am


= document.documentElement = document.body = document.head

screen shot 1939-11-27 at 4 35 57 am


innerHTML and outerHTML

let p = document.body.innerHTML;
let p = document.getElementById("id").innerHTML;

querySelector

The call to document.querySelector(css) returns the first element for the given CSS selector.
In other words, the result is the same as elem.querySelectorAll(css)[0]

@yogain123
Copy link
Author

Attributes and properties

  • elem.hasAttribute(name) – checks for existence.
  • elem.getAttribute(name) – gets the value.
  • elem.setAttribute(name, value) – sets the value.
  • elem.removeAttribute(name) – removes the attribute.
<body>
  <div id="elem" about="Elephant"></div>

  <script>
    alert( document.getElementById('elem').getAttribute('about') ); // (1) 'Elephant', reading
    document.getElementById('elem').setAttribute('test', 123); // (2), writing
    alert( document.getElementById('elem').outerHTML ); // (3), see it's there
    for (let attr of document.getElementById('elem').attributes) { // (4) list all
        alert( `${attr.name} = ${attr.value}` );
    }
  </script>
</body>

@yogain123
Copy link
Author

Introductions To Events

Mouse events:

  • click – when the mouse clicks on an element (touchscreen devices generate it on a tap).
  • contextmenu – when the mouse right-clicks on an element.
  • mouseover / mouseout – when the mouse cursor comes over / leaves an element.
  • mousedown / mouseup – when the mouse button is pressed / released over an element.
  • mousemove – when the mouse is moved.

Form element events:

  • submit – when the visitor submits a .
  • focus – when the visitor focuses on an element, e.g. on an .

Keyboard events:

  • keydown and keyup – when the visitor presses and then releases the button.

@yogain123
Copy link
Author

Bubbling and capturing

The bubbling principle is simple.

When an event happens on an element, it first runs the handlers on it, then on its parent, then all the way up on other ancestors.

<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>

http://plnkr.co/edit/ZcEbAiqTzywtFxoHdF2Z?p=info

event.target
A handler on a parent element can always get the details about where it actually happened.

  <form id="form">FORM
    <div>DIV
      <p>P</p>
    </div>
  </form>
document.getElementById("form").onclick = function(event) {
  event.target.style.backgroundColor = 'yellow';

  // chrome needs some time to paint yellow
  setTimeout(() => {
    alert("target = " + event.target.tagName + ", this=" + this.tagName);
    event.target.style.backgroundColor = ''
  }, 0);
};

Stopping bubbling
A bubbling event goes from the target element straight up. Normally it goes upwards till , and then to document object, and some events even reach window, calling all handlers on the path.
event.stopPropagation()

Capturing
There’s another phase of event processing called “capturing”. It is rarely used in real code, but sometimes can be useful.

The standard DOM Events describes 3 phases of event propagation:

  • Capturing phase – the event goes down to the element.
  • Target phase – the event reached the target element.
  • Bubbling phase – the event bubbles up from the element.

Prevent Browser default actions
event.preventDefault();

=======

<html>
<p id="check">Hello India</p>
</html>

document.querySelector("#check").addEventListener("click", function(){
  document.getElementById("check").innerHTML = "Hello World";
  //or something
});

@yogain123
Copy link
Author

Resource loading

Let’s say we need to call a function that resides in an external script.
…But how to run the function that is declared inside that script? We need to wait until the script loads, and only then we can call it.

script.onload

let script = document.createElement('script');

// can load any script, from any domain
script.src = "https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.3.0/lodash.js"
document.head.append(script);

script.onload = function() {
  // the script creates a helper function "_"
  alert(_); // the function is available

script.onerror = function() {
  alert("Error loading " + this.src); // Error loading https://example.com/404.js
};
};

@yogain123
Copy link
Author

Forms

input and textarea

input.value = "New value";
textarea.value = "New text";
input.checked = true; // for a checkbox or radio button

select and option
So the ways to set the value of a select is :

<select id="select">
   <option value="apple">Apple</option>
   <option value="pear">Pear</option>
   <option value="banana">Banana</option>
</select>
<script>
  select.value = 'banana';
</script>

onfocus and onblur

screen shot 1939-11-27 at 5 44 15 am

onchange and oninput

screen shot 1939-11-27 at 5 46 07 am


screen shot 1939-11-27 at 5 47 12 am

Event: submit

screen shot 1939-11-27 at 5 49 53 am

@yogain123
Copy link
Author

Browser environment, specs

There’s a “root” object called window

function sayHi() {
  alert("Hello");
}
window.sayHi();
alert(window.innerHeight); // inner window height

The document object gives access to the page content

/ change the background color to red
document.body.style.background = "red";

// change it back after 1 second
setTimeout(() => document.body.style.background = "", 1000);

@yogain123
Copy link
Author

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <ul id="parent">
        <li class="child">one</li>
        <li class="child">two</li>
        <li class="child">three</li>
        <li class="child">four</li>
        <li class="child">five</li>
        <li class="child">six</li>
        <li class="child">seven</li>
    </ul>


    <ul id="parent1">
        <li class="child1">one</li>
        <li class="child1">two</li>
        <li class="child1">three</li>
        <li class="child1">four</li>
        <li class="child1">five</li>
        <li class="child1">six</li>
        <li class="child1">seven</li>
    </ul>

    <ul onClick=parent2click(event) id="parent2">
        <li onClick=child2clickk(event) class="child2">one</li>
        <li onClick=child2click(event) class="child2">two</li>
        <li onClick=child2click(event) class="child2">three</li>
        <li onClick=child2click(event) class="child2">four</li>
        <li onClick=child2click(event) class="child2">five</li>
        <li onClick=child2click(event) class="child2">six</li>
        <li onClick=child2click(event) class="child2">seven</li>
    </ul>

    <script type="text/javascript">

    //========== Event In a most obvisous way ===================
    let children = Array.from(document.getElementsByClassName("child"));
    for (const child of children) {
        child.addEventListener('click',()=>{
            console.log("clicked child", child.innerHTML, "from Obvious Way");
        })        
    }

    //========== Event In a most obvisous way ===================

    //========== Event Delegation ===============================


    let parent = document.getElementById('parent1');
    parent.addEventListener('click',(event)=>{
        console.log("clicked child", event.target.innerHTML,"from Event delegation");
    })

    //========== Event Delegation ===============================


    //================ BUBBLING ===================

    function parent2click(event){
        alert("parent");
    }

    function child2click(event){
        alert("child :Stop Propagation");
        event.stopPropagation();
    }

    function child2clickk(event){
        alert("child : DO NOT Stop Propagation");
    }

    //================ BUBBLING ====22===============
</script>
</body>
</html>

@yogain123
Copy link
Author

Quick View

document.body.firstChild.nodeName

document.body.childNodes

document.body.previousElementSibling

document.body.nextElementSibling

document.body.lastElementChild

document.body.firstElementChild

@yogain123
Copy link
Author

importing bootstrap

Importing in js file

import "bootstrap/dist/css/bootstrap.min.css"

importing in css/scss file to override default

body-bf:#12332
@import "~bootstrap/dist/css/bootstrap.min.css"

@yogain123
Copy link
Author

    <style>
      ul > li:nth-child(3n + 1) {
        background-color: red; /* 1,4,7,10 */
      }
      ul > li:nth-child(3n + 2) {
        background-color: green; /* 2,5,8,11 */
      }
      ul > li:nth-child(3n + 3) {
        background-color: blue; /* 3,6,9 */
      }
    </style>

@yogain123
Copy link
Author

yogain123 commented Sep 26, 2022

@yogain123
Copy link
Author

DOM Elements

Screenshot 2022-09-26 at 7 10 35 PM

Screenshot 2022-09-26 at 7 12 23 PM

@yogain123
Copy link
Author

Node VS Elements

Screenshot 2022-09-26 at 7 13 19 PM

Screenshot 2022-09-26 at 7 15 19 PM


Screenshot 2022-09-26 at 7 16 08 PM


Screenshot 2022-09-26 at 7 18 01 PM


Screenshot 2022-09-26 at 7 18 43 PM


Screenshot 2022-09-26 at 7 19 37 PM


Screenshot 2022-09-26 at 7 20 34 PM

@yogain123
Copy link
Author

Insert Adjacent Elem

Screen Shot 2022-10-09 at 1 19 16 AM

Screen Shot 2022-10-09 at 1 19 23 AM

@yogain123
Copy link
Author

flexbox props

Screen Shot 2022-10-16 at 4 22 09 PM

Screen Shot 2022-10-16 at 4 21 45 PM

@yogain123
Copy link
Author

handle image content

object-fit: "cover"

@yogain123
Copy link
Author

yogain123 commented Nov 22, 2022

CSS layers

by default user agent define some css rule to eaach html elem, we override it by owr own style

Screen Shot 2022-11-22 at 3 07 08 PM

similary we can also define layers to css style to tell what is more important to handle specificity

the way our stack wrks is that broswr stylesheet at bottom is least speicifc, then we have layer called utilities is more specific and then we have all the code not in layer which is going to be the next most specific after utilities

Screen Shot 2022-11-22 at 3 08 14 PM


Screen Shot 2022-11-22 at 3 11 19 PM

specificity increases frm left to right

we can change speicifcty of vendr css imports

Screen Shot 2022-11-22 at 3 12 22 PM

imports in css is not a good practise because first it downloads the entire styles.css and then looks for all the css import and then it downloads them, so thigs are not parelley
so its good to put @imports in html file

Screen Shot 2022-11-22 at 3 15 19 PM


Note::
Unlike normal CSS with the layer, everything dealing with important goes in backward order (right to left)

Screen Shot 2022-11-22 at 3 17 25 PM

======

whne we gicve id to element, it gets attached to windw objetct so we can use direct , window.hola , or hola to get it
<div id="hola">Hellooooo</div>

@yogain123
Copy link
Author

@yogain123
Copy link
Author

yogain123 commented Nov 22, 2022

picture Elem

Screen Shot 2022-11-22 at 4 34 10 PM

@yogain123
Copy link
Author

yogain123 commented Feb 20, 2024

From Chatgpt

html attribute data-something -> https://chat.openai.com/share/9c200155-bdf4-47df-b62e-09114726115d

z-index -> For z-index to work, the element (or pseudo-element) must be positioned—meaning it should have position: relative, position: absolute, position: fixed, or position: sticky. By default, ::before and ::after are inline, so you will need to set their position.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment