Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #23211
| From | Cezary Tomczyk <cezary.tomczyk@gmail.com> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Get inner text of HMTL element |
| Date | 2014-02-22 23:38 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <leb8sc$23n$5@dont-email.me> (permalink) |
| References | (1 earlier) <5308ed02$0$9512$9b4e6d93@newsspool1.arcor-online.net> <leav3t$aa4$5@dont-email.me> <1633562.pVBMJriu5e@PointedEars.de> <leb1q1$r9i$1@dont-email.me> <1780761.mlrEmVmIzg@PointedEars.de> |
Thomas 'PointedEars' Lahn wrote:
> Cezary Tomczyk wrote:
[...]
>> function get_inner_text(elm) {
>> var txt = '';
>> if (elm && elm.childNodes && elm.childNodes.length > 0) {
>> txt = elm.data || elm.textContent || elm.innerText;
>> }
>> return txt;
>> }
>
> Better (but unnecessarily inefficient: consider the “firstChild” property),
> but see below.
True. I've rewritten it to:
function get_inner_text(elm) {
var txt = '';
if (elm && elm.firstChild) {
txt = elm.textContent || elm.innerText;
}
return txt;
}
There is no "elm.data". I've explained it below.
[...]
>>> What is the result of
>>>
>>> "" || void 0 || void 0
>>>
>>> ?
>>
>> "" is converted to "false" boolean
>> void 0 is evaluated to "undefined" and then to "false" boolean
>>
>> Because all of them are converted to "false" boolean then "undefined" by
>> default is returned.
>>
>> How about that?
>
> The final operand¹ is not converted, and no operand is actually converted
> (instead of just internally for the purpose of determining shortcut
> evaluation). And nothing is *returned*. Otherwise you have described it
> correctly.
Right. I was thinking about default return value when function has no
defined "return" statement. But in this case there is no function at all.
>> function get_inner_text(elm) {
>> var txt = (elm.data || elm.textContent || elm.innerText);
>> if (!txt) {
>> txt = '';
>> }
>> return txt;
>> }
>
> Much the same could be achieved with
>
> var text = elm.data || elm.textContent || elm.innerText || "";
>
> and it would still be wrong.
:-( There is no "elm.data".
> For a start, one wonders how you got the idea that an *element* object had a
> (built-in, standards-compliant) “data” property to yield the text content of
> the represented element.
I have no idea how it happened. I've already worked with the property
"data", but I forgot that it apply only to TextNode object
(http://msdn.microsoft.com/en-us/library/ie/cc304097%28v=vs.85%29.aspx).
After longer research, experimenting and reading
(http://www.kellegous.com/j/2013/02/27/innertext-vs-textcontent/) I
think I can build something like this:
var get_inner_text;
if (typeof document.getElementsByTagName('html')[0].textContent ===
'string') {
get_inner_text = function(node) {
return node.textContent;
};
} else if (document.createTreeWalker) {
get_inner_text = function(root) {
var w = document.createTreeWalker(root, 4, null, false),
result = '';
while (w.nextNode()) {
result += w.currentNode.data;
}
return result;
};
} else {
get_inner_text = function(node) {
var txt = '';
node = node.firstChild;
while (node) {
if (node.nodeType === 3 || node.nodeType === 4) {
txt += node.data;
} else if (node.nodeType === 1){
txt += get_inner_text(node);
}
node = node.nextSibling;
}
return txt;
};
}
How about that?
> _______
> ¹ Colloquially speaking; actually, we are looking at only one “||” operation
> whose first operand is the result of another one: “a || b || c” is parsed
> like “(a || b) || c” due to the production
>
> | LogicalORExpression(NoIn) :
> | LogicalANDExpression(NoIn)
> | LogicalORExpression || LogicalANDExpression(NoIn)
>
> <http://ecma-international.org/ecma-262/5.1/#sec-11.11>
Thanks.
--
Cezary Tomczyk
http://www.ctomczyk.pl/
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Get inner text of HMTL element Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2014-02-22 18:21 +0100
Re: Get inner text of HMTL element Martin Honnen <mahotrash@yahoo.de> - 2014-02-22 19:31 +0100
Re: Get inner text of HMTL element Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2014-02-22 20:51 +0100
Re: Get inner text of HMTL element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-22 21:11 +0100
Re: Get inner text of HMTL element Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2014-02-22 21:37 +0100
Re: Get inner text of HMTL element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-22 22:05 +0100
Re: Get inner text of HMTL element Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2014-02-22 23:38 +0100
Re: Get inner text of HMTL element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-23 10:43 +0100
Re: Get inner text of HMTL element Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2014-02-23 14:50 +0100
Re: Get inner text of HMTL element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-23 15:48 +0100
Re: Get inner text of HMTL element Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2014-02-24 21:01 +0100
Re: Get inner text of HMTL element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-24 22:34 +0100
Re: Get inner text of HMTL element Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2014-02-27 21:41 +0100
Re: Get inner text of HMTL element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-27 22:32 +0100
Re: Get inner text of HMTL element Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2014-02-28 13:44 +0100
Re: Get inner text of HMTL element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-28 16:54 +0100
Re: Get inner text of HMTL element Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2014-03-04 20:51 +0100
Re: Get inner text of HMTL element ompalompasales2@gmail.com - 2014-03-04 12:59 -0800
Re: Get inner text of HMTL element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-22 20:59 +0100
Re: Get inner text of HMTL element Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2014-02-22 22:23 +0100
Re: Get inner text of HMTL element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-22 23:32 +0100
Re: Get inner text of HMTL element Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2014-02-22 21:27 -0300
Re: Get inner text of HMTL element "se" <se@no-one.here> - 2014-02-23 05:35 +0100
Re: Get inner text of HMTL element Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2014-02-23 15:29 -0300
Re: Get inner text of HMTL element "se" <se@no-one.here> - 2014-02-23 21:01 +0100
Re: Get inner text of HMTL element Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2014-02-23 20:02 +0100
Re: Get inner text of HMTL element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-23 21:55 +0100
Re: Get inner text of HMTL element Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2014-02-24 08:37 +0100
Re: Get inner text of HMTL element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-24 12:30 +0100
Re: Get inner text of HMTL element Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2014-02-24 15:09 -0300
Re: Get inner text of HMTL element Christoph Michael Becker <cmbecker69@arcor.de> - 2014-02-24 19:43 +0100
Re: Get inner text of HMTL element Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2014-02-24 18:12 -0300
Re: Get inner text of HMTL element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-24 22:51 +0100
Re: Get inner text of HMTL element Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-02-25 19:49 +0000
Re: Get inner text of HMTL element Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2014-02-26 05:20 -0300
Re: Get inner text of HMTL element "se" <se@no-one.here> - 2014-02-26 17:10 +0100
OT - Re: Get inner text of HMTL element Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2014-02-26 15:48 -0300
Re: OT - Re: Get inner text of HMTL element "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-02-26 22:43 +0100
Re: OT - Re: Get inner text of HMTL element Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-02-27 15:00 +0000
Re: OT - Re: Get inner text of HMTL element "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-02-28 10:50 +0100
Re: OT - Re: Get inner text of HMTL element "se" <se@no-one.here> - 2014-02-27 10:57 +0100
Re: Get inner text of HMTL element Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-02-27 15:46 +0000
csiph-web