Open briefy opened 7 years ago
let's talk about the event.preventDefault()
.
you know that when we registered event listeners using target.addEventListener
, they are called in the sequence that they are registered by.So, if we call event.preventDefault()
,actually we will cancel the event.
if the event is a self-dispatched event(which is not created and initiated by the browser ), target.dispatchEvent(event)
will return false.
when we trigger an event in a browser, it is that we notify the browser to do its default behavior and we
can do what we wanna do in the callback hook by the way.So if we call the event.preventDefault()
,
actually we tell the browser to stop doing its default behavior. You know sometimes,on some elements
there is no default behavior,like div
with click
,and event.preventDefatul()
prevents nothing.
in the downward comments, I will elaborate on that when an event is triggered what will the browser do by default. when you call event.preventDefault()
, the browser will stop to do the jobs it should be done by default.BUT, it won't stop the propagation and other listeners from being called. That is what
event.stopPropgation() and event.stopImmediatePropagation()
will do.
popstate Manipulating the browser history Browsers tend to handle the popstate event differently on page load. Chrome (prior to v34) and Safari (prior to 10.0) always emit a popstate event on page load, but Firefox doesn't
IE and Edge < 14 do not fire the popstate event when the URL's hash value changes
calling history.pushState()
orhistory.replaceState()
won't trigger a popstate event
what triggers apopstate event
:
window.loaction=/path/change/or#fragment-change
not search change it will reload the pagehistory.back
history.go
history.forward
EventTarget
EventTarget is an interface implemented by objects that can receive events and may have listeners for them.
common EventTargets are
an EventTarget support the following methods.
addEventListener
NOTES: There is a create-init-dispatch process of dealing with events.
document.createEvent
Event MouseEvent KeyBoardEent FocusEvent
)initEvent initMouseEvent initUIEvent
methodsa simple implementation of EventTarget