Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Updated FAQ

0 views
Skip to first unread message

Garrett Smith

unread,
Jan 3, 2010, 2:44:01 AM1/3/10
to
How do I trim whitespace?
Change code to coerce the thisArg to String, as per ES5
- Bozhilov

How do I POST a form to a new window?
Change example to use method="post"
-Saarikumpu

How do I disable the right mouse button?
Fix the grammar
- Stockton

How can I see in javascript if a web browser accepts cookies?
Replace links to cookiecentral and galasoft with MDC link
- Lahn

If I missed something, say something.

Thanks.
--
Garrett
comp.lang.javascript FAQ: http://jibbering.com/faq/

Asen Bozhilov

unread,
Jan 3, 2010, 9:01:42 AM1/3/10
to
Garrett Smith wrote:
> How do I trim whitespace?
>    Change code to coerce the thisArg to String, as per ES5

| return (this + "").replace(/^\s+|\s+$/g, '');

Should be:
("" + this)

See in JScript:

var o = {toString : null, valueOf : null};
window.alert('' + o); //empty string
window.alert(o + ''); //[object]

var xhr = new ActiveXObject('Microsoft.XMLHTTP');
window.alert('' + xhr); //empty string
window.alert(xhr + ''); //undefined

In your thread about "Code Guidelines" i payed attention about that:
<URL: http://groups.google.bg/group/comp.lang.javascript/msg/1528f612e31f09fe>

Happy New Year!

David Mark

unread,
Jan 3, 2010, 9:35:50 AM1/3/10
to
On Jan 3, 2:44 am, Garrett Smith <dhtmlkitc...@gmail.com> wrote:
> How do I trim whitespace?
>    Change code to coerce the thisArg to String, as per ES5
>   - Bozhilov
>
> How do I POST a form to a new window?
>    Change example to use method="post"
>   -Saarikumpu
>
> How do I disable the right mouse button?
>    Fix the grammar
>    - Stockton
>
> How can I see in javascript if a web browser accepts cookies?
>    Replace links to cookiecentral and galasoft with MDC link
>    - Lahn
>
> If I missed something, say something.
>
> Thanks.

The Contributors section in the Notes is years out of date.

Garrett Smith

unread,
Jan 3, 2010, 10:45:50 PM1/3/10
to
David Mark wrote:
> On Jan 3, 2:44 am, Garrett Smith <dhtmlkitc...@gmail.com> wrote:
>> How do I trim whitespace?

[...]


>> If I missed something, say something.
>>
>> Thanks.
>
> The Contributors section in the Notes is years out of date.

Certainly is. Anyone want to propose a draft?

Garrett Smith

unread,
Jan 4, 2010, 2:14:14 AM1/4/10
to

Right, I see that now. Weird! It seems that with Host object in IE, the
+ operator behaves differently.

I've changed the example to:-
| if(!String.prototype.trim) {
| String.prototype.trim = function() {
| // XXX IE ("" + this) and not (this + "") for Host obj
|
//http://groups.google.com/group/comp.lang.javascript/msg/fe387847c719119a
| return ("" + this).replace(/^\s+|\s+$/g, '');
| };
| }

Look OK?

kangax

unread,
Jan 4, 2010, 2:18:33 AM1/4/10
to

^^ ^^

Would be better to use either double or single quotes, consistently :)

[...]

--
kangax

David Mark

unread,
Jan 4, 2010, 2:32:54 AM1/4/10
to
On Jan 3, 10:45 pm, Garrett Smith <dhtmlkitc...@gmail.com> wrote:
> David Mark wrote:
> > On Jan 3, 2:44 am, Garrett Smith <dhtmlkitc...@gmail.com> wrote:
> >> How do I trim whitespace?
>
> [...]>> If I missed something, say something.
>
> >> Thanks.
>
> > The Contributors section in the Notes is years out of date.
>
> Certainly is. Anyone want to propose a draft?

I propose you add my name. :)

Asen Bozhilov

unread,
Jan 4, 2010, 2:36:42 AM1/4/10
to
Garrett Smith wrote:

> Right, I see that now. Weird! It seems that with Host object in IE, the
> + operator behaves differently.

Yes, but addition operator have different behavior not only with host
objects. See below with native object:

var o = {toString : null, valueOf : null};

try {
window.alert(String(o));
}catch(e) {
window.alert(e instanceof TypeError); //[[DefaultValue]] throw
TypeError
}

window.alert('' + o); //empty primitive string value


window.alert(o + ''); //[object]

They have much more complex algorithm, from presented in ECMA262-3
documentation. And they, definitely doesn't follow specification in
that point.

Thomas 'PointedEars' Lahn

unread,
Jan 4, 2010, 2:47:10 AM1/4/10
to
Garrett Smith wrote:

> Asen Bozhilov wrote:
>> var xhr = new ActiveXObject('Microsoft.XMLHTTP');
>> window.alert('' + xhr); //empty string
>> window.alert(xhr + ''); //undefined
>>
>> In your thread about "Code Guidelines" i payed attention about that:
>> <URL:
>> http://groups.google.bg/group/comp.lang.javascript/msg/1528f612e31f09fe>
>>
>
> Right, I see that now. Weird! It seems that with Host object in IE, the
> + operator behaves differently.

Yet another bug triggered by unwise programming.

> I've changed the example to:-
> | if(!String.prototype.trim) {

Should have a space after `if'; it is not a function call.

> | String.prototype.trim = function() {

See, if you used a method to do this instead you would not be in such a
need of writing identifiers twice. That is one advantage.

> | // XXX IE ("" + this) and not (this + "") for Host obj

^^^^
Should be lowercase (that error goes like a red thread through all your
postings; perhaps you have not understood the concept behind it at all?),
and "object" should not be abbreviated like this.

Should be a multi-line documentation comment using a proper task tag:

/* NOTE: JScript requires ("" + this) for host objects */

(But this would be superfluous without implicit typecasting anyway.)

> |
>
//http://groups.google.com/group/comp.lang.javascript/msg/fe387847c719119a
> | return ("" + this).replace(/^\s+|\s+$/g, '');
> | };
> | }
>
> Look OK?

No. There is also no need of or advantage in relying on implicit
typecasting.


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

Asen Bozhilov

unread,
Jan 4, 2010, 3:16:46 AM1/4/10
to
Thomas 'PointedEars' Lahn wrote:

> Yet another bug triggered by unwise programming.

No. That is yet another bug from Microsoft ECMA-262 implementation,
called with name JScript. In normal case:

new ActiveXObject('Microsoft.XMLHTTP') + ''; //expected TypeError

And nobody suggest to do it that.

> No.  There is also no need of or advantage in relying on implicit
> typecasting.

| ECMA 5 15.5.4.20
| NOTE The trim function is intentionally generic;
| it does not require that its this value be a
| String object.
| Therefore, it can be transferred
| to other kinds of objects for use as a method.

Garrett Smith

unread,
Jan 4, 2010, 12:33:52 PM1/4/10
to
Asen Bozhilov wrote:
> Garrett Smith wrote:
>
>> Right, I see that now. Weird! It seems that with Host object in IE, the
>> + operator behaves differently.
>
> Yes, but addition operator have different behavior not only with host
> objects. See below with native object:
>
> var o = {toString : null, valueOf : null};
That would be expected to throw an error, although it does explain
the problem with Host object. THe problem is that the error occurs in
calling toString and valueOf throws.

ActiveXObject has neither toString nor value of and so String
concatenation throws there.

window.external.toString; // undefined

Comment changed to:-
// XXX JScript ("" + this), not (this + "") for objects w/o toString.
//http://groups.google.com/group/comp.lang.javascript/msg/fe387847c719119a

Thomas 'PointedEars' Lahn

unread,
Jan 4, 2010, 12:43:47 PM1/4/10
to
Garrett Smith wrote:

> Asen Bozhilov wrote:
>> Garrett Smith wrote:
>>> Right, I see that now. Weird! It seems that with Host object in IE, the
>>> + operator behaves differently.
>>
>> Yes, but addition operator have different behavior not only with host
>> objects. See below with native object:
>>
>> var o = {toString : null, valueOf : null};
> That would be expected to throw an error, although it does explain
> the problem with Host object.

It does not; check your assumptions. And there is that ominous "Host
object" again -- when will you get it?

> THe problem is that the error occurs in
> calling toString and valueOf throws.

Parse error.

> ActiveXObject has neither toString nor value of and so String

Has what?

> concatenation throws there.

Throws what? And no, that is _not_ necessarily the reason why.



> window.external.toString; // undefined
>
> Comment changed to:-
> // XXX JScript ("" + this), not (this + "") for objects w/o toString.
>
//http://groups.google.com/group/comp.lang.javascript/msg/fe387847c719119a

Neither writing nor reading appear to be the forté of the current FAQ
maintainer -- goodnight, cljs.


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300...@news.demon.co.uk>

Asen Bozhilov

unread,
Jan 4, 2010, 1:18:48 PM1/4/10
to
Thomas 'PointedEars' Lahn wrote:

> Neither writing nor reading appear to be the forté of the current FAQ
> maintainer -- goodnight, cljs.

So what? I told you before. If you have any problem in real life, go
outside and take a fresh air, before start blaming everyone in
c.l.js.

Dr J R Stockton

unread,
Jan 5, 2010, 3:54:56 PM1/5/10
to
In comp.lang.javascript message <hhro9k$mn9$1...@news.eternal-
september.org>, Sun, 3 Jan 2010 19:45:50, Garrett Smith
<dhtmlk...@gmail.com> posted:

>>
>> The Contributors section in the Notes is years out of date.
>Certainly is. Anyone want to propose a draft?

I shall be reasonably content with anything that does not include my
name.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (RFCs 5536/7)
Do not Mail News to me. Before a reply, quote with ">" or "> " (RFCs 5536/7)

Dr J R Stockton

unread,
Jan 5, 2010, 4:17:42 PM1/5/10
to
In comp.lang.javascript message <hhs4gd$cc3$1...@news.eternal-
september.org>, Sun, 3 Jan 2010 23:14:14, Garrett Smith
<dhtmlk...@gmail.com> posted:

>
>Right, I see that now. Weird! It seems that with Host object in IE, the
>+ operator behaves differently.
>
>I've changed the example to:-
>| if(!String.prototype.trim) {
>| String.prototype.trim = function() {
>| // XXX IE ("" + this) and not (this + "") for Host obj
>| //http://groups.google.com/group/comp.lang.javascript/msg/fe387847c71
>9119a
>| return ("" + this).replace(/^\s+|\s+$/g, '');
>| };
>| }
>
>Look OK?

No. It is customary to convert to String with +"" and not ""+ - which
means that code correctly following the FAQ and working in 'all'
browsers may later get 'tidied' into the usual form and then work in
'all' browsers but one. If it works properly in at least as many
browsers, I suggest
return String(this).replace(/^\s+|\s+$/g, "");
which is only one character longer.


==== On the Web,

function countdown(){
var today=new Date()
var todayy=today.getYear()
if (todayy < 1000)
todayy+=1900
var todaym=today.getUTCMonth()
var todayd=today.getUTCDate()
...

still has not been corrected. But there are obvious sibling instances
of function countdown() which lack the prime fault, so it probably was
not in the original. Another part of the file has a brace of 1800-year
errors.

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links;
Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.

JR

unread,
Jan 5, 2010, 9:47:28 PM1/5/10
to

It looks completely odd to me. If you test for String.prototype.trim,
then there's no sense in coercing the argument to string, as in (this
+ '') or ('' + this), because the trim method should only be used with
strings. The trim() method proposed in the FAQ looks like a mutant,
and I doubt that the FF native code does such a coercion.

I think that the developer should test whether the argument is a
string before trying to use this method, I mean that kind of check
should be done outside the trim method.

By the way, there's an interesting article about this subject at:
http://blog.stevenlevithan.com/archives/faster-trim-javascript

A very fast regex was proposed there (trim1)

return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');

The reasons are explained in the article.

Cheers,
JR

Garrett Smith

unread,
Jan 5, 2010, 10:55:01 PM1/5/10
to
JR wrote:
> On Jan 4, 5:14 am, Garrett Smith <dhtmlkitc...@gmail.com> wrote:
>> Asen Bozhilov wrote:
>>> Garrett Smith wrote:
>>>> How do I trim whitespace?
>>>> Change code to coerce the thisArg to String, as per ES5

[...]

>> Look OK?
>
> It looks completely odd to me. If you test for String.prototype.trim,
> then there's no sense in coercing the argument to string, as in (this
> + '') or ('' + this), because the trim method should only be used with
> strings. The trim() method proposed in the FAQ looks like a mutant,
> and I doubt that the FF native code does such a coercion.
>

You should check those doubts against the specification.

Asen was correct in bringing this up and in the workaround for the
JScript bug he mentioned.

0 new messages