Closed jamesbrewerdev closed 10 years ago
Why not the following? Shorter & sweeter.
return tmp.textContent || tmp.innerText || "";
It will return the same result, but I think my solution better explains that tmp.innerText
should only be returned if typeof tmp.textContent != 'undefined'
is true
.
Returning an empty string is more of a hack that hides the problem instead of fixing it.
If you feel strongly. :)
What about !== vs !=? I can't approve a merge, btw, so I'm just chatting.
Internet Explorer introduced element.innerText a while back which retrieves the text from a DOM node. All other major browsers have agreed to use Node.textContent instead. While most of them also support element.innerText, Firefox does not.
The trunk8 plugin for jQuery uses the check tmp.textContent || tmp.innerText to retrieve the text from a given node. This won't work in Firefox when
tmp
has no content -- specifically whenhtml
is an empty string -- becausetmp.textContent
returns""
(an empty string, which is falsey in JavaScript) andtmp.innerText
returnsundefined
. Because of JavaScript's short circuiting feature, the expressiontmp.textContent || tmp.innerText
then returnsundefined
. This caused an error in one code which ultimately resulted in the page not rendering entirely.This fix checks to see whether the browser supports Node.textContent and, if so, returns
tmp.textContent
. Otherwise, returntmp.innerText
.