Notes from reading DOM Enlightenment
Saturday, February 10, 2024Read here: DOM Enlightenment
HTML things:
node.isEqualNode(otherNode)
to check ifnode
in the DOM is the same asotherNode
document.referrer
,document.compatMode
exists???element.attributes
returns aNamedNodeMap
- primary usage via
get/set/has/removeAttribute()
- get/set/del via
getNamedItem
,setNamedItem
,removeNamedItem
- primary usage via
element.classList
returns DOMTokenListelement.dataset
mapsdata-foo-foo
tofooFoo
in theDOMStringMap
document.forms
,document.images
,document.links
returns an HTMLCollection of all those elements in the document (damn)offsetLeft
/offsetTop
returns the offset pixel value of the element from itsoffsetParent
- the
offsetParent
is any ancestor with position which is not static (hence, eitherabsolute
,relative
,fixed
, etc)
- the
- for viewport relative offset or element width/height (including margin),
element.getBoundingClientRect()
works - for element width/height (content+padding),
element.clientWidth
,element.clientHeight
- for topmost element in viewport using a point,
document.elementFromPoint(x,y)
- change the entire inline styles of an element using
element.cssText
as well aselement.setAttribute()
- get the current computed style values using
getComputedStyle(element)
- create a
Text
node usingdocument.createTextNode())
- manipulate it via
appendData()
,deleteData()
,insertData()
, etc
- manipulate it via
element.innerText
vstextNode.textContent
innerText
is CSS aware, and won't return hidden texttextContent
will return what is in the document after removing markup
DocumentFragment
for creating insertable nodes, transfers live objects to document, whilecreateElement
ones do not. only inserts the content of the Fragment instead of the Fragment itself (usecloneNode(fragment)
to preserve the original fragment structure)
CSS Things:
- to get a style declaration, try
document.styleSheets[0].cssRules[0]
appropriately- to insert,
styleElement.sheet.insertRule(rule)
- to disable a stylesheet,
styleElement.disabled = true
- to insert,
JS Things:
- JS parsing synchronous by default
defer
on<script>
helps defer (lol) the download and execution of external JS till browser finishes parsing the closinghtml
tagasync
on<script>
prevents blocking of the html parsing, and load in parallel- for dom manipulation,
<script>
tag order matters - capture phase -> DOM trunk to branch (
event.target
) - bubbling phase -> branch (
event.target
) to trunk this
inside event listener onaddEventListener
will refer to the node/object the event is attached toevent.target
is the target of the event,this
is the invoker of the event callback- cancel browser default events with
preventDefault()
stopPropagation()
prevents the capture phase and bubbling phase, only the event listener attached to element will be calledstopImmediatePropagation()
will stop the phases as well as any events attached after the event callback containingstopImmediatePropagation
- custom events via
document.createEvent()
,initCustomEvent()
,dispatchEvent()
- event delegation to handle multiple event targets leveraging the bubbling and capture of events