Opened 5 years ago
Closed 5 years ago
#31027 closed Cleanup/optimization (fixed)
Prefer DOM object properties over Element.getAttribute() and Element.setAttribute().
Reported by: | Jon Dufresne | Owned by: | nobody |
---|---|---|---|
Component: | contrib.admin | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Some JavaScript code uses Element.getAttribute()
or Element.setAttribute()
to access data that is available through a DOM object property. In general, the properties should be preferred as they are always normalized values and typed. The getAttribute()
/setAttribute()
are an abstraction over the serialized text representation.
Change History (3)
comment:1 by , 5 years ago
Has patch: | set |
---|
comment:2 by , 5 years ago
Summary: | Prefer DOM object properties over Element.getAttribute() and Element.setAttribute() → Prefer DOM object properties over Element.getAttribute() and Element.setAttribute(). |
---|---|
Triage Stage: | Unreviewed → Accepted |
I agree that we should prefer DOM properties but we need to be aware of differences and caveats (see comparison). For example href
is quite tricky because as a DOM property it's always a full URL, even if the attribute contains a relative URL or just a #
and of course on IE it doesn't work that way.
https://github.com/django/django/pull/12138