Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #14964 > unrolled thread

Built-in string functions clobber strings in array?

Started byterphenyl <terphenyl@hotmail.com>
First post2012-07-08 20:01 -0700
Last post2012-07-09 22:09 +0200
Articles 4 — 4 participants

Back to article view | Back to comp.lang.javascript


Contents

  Built-in string functions clobber strings in array? terphenyl <terphenyl@hotmail.com> - 2012-07-08 20:01 -0700
    Re: Built-in string functions clobber strings in array? Gregor Kofler <usenet@gregorkofler.com> - 2012-07-09 10:36 +0200
      Re: Built-in string functions clobber strings in array? terphenyl@hotmail.com - 2012-07-09 12:39 -0700
      Re: Built-in string functions clobber strings in array? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-07-09 22:09 +0200

#14964 — Built-in string functions clobber strings in array?

Fromterphenyl <terphenyl@hotmail.com>
Date2012-07-08 20:01 -0700
SubjectBuilt-in string functions clobber strings in array?
Message-ID<2c985566-9108-4346-873c-a709466fa795@googlegroups.com>
In an test html page I have 5 lines like:
<a href="http://www.yahoo.com">Yahoo</a>
<a href="http://www.google.com">Google</a>
<a href="mailto:?Bcc=k-smith@abcd.edu">Email me</A>
<a href="http://www.hotmail.com">Google</a>
<a href="mailto:?Bcc=k-jones@abcd.edu">Email him</A>
and I want my javascript to return the URLs as strings I can
detect, say, the mailto: lines.  
This almost works:

var allLinks = content.document.getElementsByTagName("a");
var txt = '';
var stxt = '';

 for( var jj=0, kk=0; jj < allLinks.length; jj++)
            {
             document.write("<br>" + jj + "    ");
             //document.write(allLinks[jj]);
             txt = allLinks[jj];
             document.write(txt);
             stxt = txt.substring(4,8);
             // var n = s.indexOf("Bcc");
           }
This will write on the page the index (jj), and the URL for the 
[0] index element of allLinks (as the copied string txt), and then
just stop, even though there are 5 elements, 0...4.
WITHOUT the txt.substring(4,8) or indexOf, or any built-in string
function, all 5 URLs are displayed just fine (with the indexes).

I thought the elements of allLinks were strings, and, to be conservative
copied the strings to a different variable (txt) and then tried a string operation (or two) on that variable.  

I get all five strings in the allLinks array to print correctly,
unless I then try a built-in string function on the string.
 

[toc] | [next] | [standalone]


#14965

FromGregor Kofler <usenet@gregorkofler.com>
Date2012-07-09 10:36 +0200
Message-ID<jte575$u4f$1@dont-email.me>
In reply to#14964
Am 2012-07-09 05:01, terphenyl meinte:
> In an test html page I have 5 lines like:
> <a href="http://www.yahoo.com">Yahoo</a>
> <a href="http://www.google.com">Google</a>
> <a href="mailto:?Bcc=k-smith@abcd.edu">Email me</A>
> <a href="http://www.hotmail.com">Google</a>
> <a href="mailto:?Bcc=k-jones@abcd.edu">Email him</A>
> and I want my javascript to return the URLs as strings I can
> detect, say, the mailto: lines.
> This almost works:
>
> var allLinks = content.document.getElementsByTagName("a");
> var txt = '';
> var stxt = '';
>
>   for( var jj=0, kk=0; jj < allLinks.length; jj++)
>              {
>               document.write("<br>" + jj + "    ");
>               //document.write(allLinks[jj]);
>               txt = allLinks[jj];
>               document.write(txt);
>               stxt = txt.substring(4,8);
>               // var n = s.indexOf("Bcc");
>             }
> This will write on the page the index (jj), and the URL for the
> [0] index element of allLinks (as the copied string txt), and then
> just stop, even though there are 5 elements, 0...4.
> WITHOUT the txt.substring(4,8) or indexOf, or any built-in string
> function, all 5 URLs are displayed just fine (with the indexes).
>
> I thought the elements of allLinks were strings

allLLinks is a collection of DOM elements.

, and, to be conservative
> copied the strings to a different variable (txt) and then tried a string operation (or two) on that variable.

An internal toString() method is invoked (which - here in FF - gives you 
the href attribute value of the anchor). However, they are not strings 
and don't provide string methods. Try something like allLinks[jj].href 
which holds a string.

Gregor


-- 
http://vxweb.net

[toc] | [prev] | [next] | [standalone]


#14977

Fromterphenyl@hotmail.com
Date2012-07-09 12:39 -0700
Message-ID<e5f4b51e-b33a-4afa-b60c-8875edd2ec2b@googlegroups.com>
In reply to#14965
On Monday, July 9, 2012 4:36:52 AM UTC-4, Gregor Kofler wrote:
> Am 2012-07-09 05:01, terphenyl meinte:
> > In an test html page I have 5 lines like:
> > <a href="http://www.yahoo.com">Yahoo</a>
> > <a href="http://www.google.com">Google</a>
> > <a href="mailto:?Bcc=k-smith@abcd.edu">Email me</A>
> > <a href="http://www.hotmail.com">Google</a>
> > <a href="mailto:?Bcc=k-jones@abcd.edu">Email him</A>
> > and I want my javascript to return the URLs as strings I can
> > detect, say, the mailto: lines.
> > This almost works:
> >
> > var allLinks = content.document.getElementsByTagName("a");
> > var txt = '';
> > var stxt = '';
> >
> >   for( var jj=0, kk=0; jj < allLinks.length; jj++)
> >              {
> >               document.write("
> " + jj + "    ");
> >               //document.write(allLinks[jj]);
> >               txt = allLinks[jj];
> >               document.write(txt);
> >               stxt = txt.substring(4,8);
> >               // var n = s.indexOf("Bcc");
> >             }
> > This will write on the page the index (jj), and the URL for the
> > [0] index element of allLinks (as the copied string txt), and then
> > just stop, even though there are 5 elements, 0...4.
> > WITHOUT the txt.substring(4,8) or indexOf, or any built-in string
> > function, all 5 URLs are displayed just fine (with the indexes).
> >
> > I thought the elements of allLinks were strings
> 
> allLLinks is a collection of DOM elements.
> 
> , and, to be conservative
> > copied the strings to a different variable (txt) and then tried a string operation (or two) on that variable.
> 
> An internal toString() method is invoked (which - here in FF - gives you 
> the href attribute value of the anchor). However, they are not strings 
> and don't provide string methods. Try something like allLinks[jj].href 
> which holds a string.
> 
> Gregor
> 
> 
> -- 
> http://vxweb.net

Thank you.  I ran across something like this a long time ago using the C++ STL <string> classes where the variable held a pointer that would print the string, but you had to de-reference a pointer to do work on the characters.  Javascript is new to me, and there is a lot to it.
Thank you again, Gregor.

--jim    

[toc] | [prev] | [next] | [standalone]


#14978

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2012-07-09 22:09 +0200
Message-ID<2146299.mvXUDI8C0e@PointedEars.de>
In reply to#14965
Gregor Kofler wrote:

> Am 2012-07-09 05:01, terphenyl meinte:
>> var allLinks = content.document.getElementsByTagName("a");
>> […]
>>
>> I thought the elements of allLinks were strings
> 
> allLLinks is a collection of DOM elements.

It refers to a NodeList of references to DOM element objects, which in the 
Gecko DOM also implements the HTMLCollection interface (for reasons I do not 
understand).

> , and, to be conservative
>> copied the strings to a different variable (txt) and then tried a string
>> operation (or two) on that variable.
> 
> An internal toString() method is invoked (which - here in FF - gives you
> the href attribute value of the anchor). However, they are not strings
> and don't provide string methods. Try something like allLinks[jj].href
> which holds a string.

Caveat: The value of the `href' property, and the return value of the 
toString() method, might not reflect the value of the `href' attribute; in 
fact, it may represent the (absolute) URI for a given (relative) URI-
reference (see RFC 3986).  In order to be sure about the value, you would 
need to use getAttribute("href"), but there is IE/MSHTML.

Therefore, ISTM that you need to strip the protocol, host, and path part of 
the current document from the property value before you attempt a match.


PointedEars
-- 
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web