HighDots Forums  

Re: Formatting strings

Javascript JavaScript language (comp.lang.javascript)


Discuss Re: Formatting strings in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #11  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: Formatting strings - more complex! - 11-07-2003 , 09:29 AM






Nige wrote:

Quote:
Cracked it! My JS book (O'Reilly) implies that window.location returns a
string, but it doesn't!
(window.)location is in recent user agents a string value *and* an
object with properties like `href', `protocol', `path', `hash' aso.

Here, in Mozilla/5.0 rv:1.5 and IE 6.0 SP-1, it is. Which user-agent
are you testing with?


PointedEars



Reply With Quote
  #12  
Old   
Lasse Reichstein Nielsen
 
Posts: n/a

Default Re: Formatting strings - more complex! - 11-07-2003 , 09:58 AM






Thomas 'PointedEars' Lahn <PointedEars (AT) web (DOT) de> writes:

Quote:
(window.)location is in recent user agents a string value *and* an
object with properties like `href', `protocol', `path', `hash' aso.

Here, in Mozilla/5.0 rv:1.5 and IE 6.0 SP-1, it is. Which user-agent
are you testing with?
In my IE6, it is only an object. It doesn't have the methods from
String.prototype (e.g., charAt), and its typeof is "object".

If you convert it to a string, either with String(location),
location.toString() or ""+location, the resulting string contains
the URL, but that just means that it can be converted to a string.

There is some magic to the location object, though, since *assigning*
to it will really assign to location.href.

As a curiosity, in Opera 7 the location.valueOf method is the same
as the location.toString.

/L
--
Lasse Reichstein Nielsen - lrn (AT) hotpop (DOT) com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'


Reply With Quote
  #13  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: Formatting strings - more complex! - 11-07-2003 , 10:25 AM



Lasse Reichstein Nielsen wrote:

Quote:
Thomas 'PointedEars' Lahn <PointedEars (AT) web (DOT) de> writes:
(window.)location is in recent user agents a string value *and* an
object with properties like `href', `protocol', `path', `hash' aso.

Here, in Mozilla/5.0 rv:1.5 and IE 6.0 SP-1, it is. Which user-agent
are you testing with?

In my IE6, it is only an object. It doesn't have the methods from
String.prototype (e.g., charAt), and its typeof is "object".
You're right, the same goes for my UAs. What I meant was that
it returns also a URI string (due to its toString() method) in
the right context.

In contrast, in older UAs like Opera 6 (IIRC), `location' stores
only a primitive string value.

Quote:
If you convert it to a string, either with String(location),
location.toString() or ""+location, the resulting string contains
the URL, but that just means that it can be converted to a string.
ACK

Quote:
There is some magic to the location object, though, since *assigning*
to it will really assign to location.href.
There is no magic involved See
http://devedge.netscape.com/library/.../location.html


Quote:
As a curiosity, in Opera 7 the location.valueOf method is the same
as the location.toString.
Same in Mozilla/5.0 and IE 6.0 SP-1, as expected.


PointedEars



Reply With Quote
  #14  
Old   
Lasse Reichstein Nielsen
 
Posts: n/a

Default Re: Formatting strings - more complex! - 11-07-2003 , 10:47 AM



Thomas 'PointedEars' Lahn <PointedEars (AT) web (DOT) de> writes:

Quote:
In contrast, in older UAs like Opera 6 (IIRC), `location' stores
only a primitive string value.
YRI (You Remember Incorrectly
In Opera 4, 5 and 6, location is an object. I don't have Opera 3 installed,
and Opera 2 doesn't support Javascript.

The same goes for Netscape 4 and 3. You have to go back to Netscape 2
to find a location property that is a plain string.

Quote:
There is some magic to the location object, though, since *assigning*
to it will really assign to location.href.

There is no magic involved See
http://devedge.netscape.com/library/.../location.html
Tha qualified as "magic" to me (something that depends on internal
code and cannot be implemented by a Javscript programmer, like the
array length property [1]).

It says:
---
If you assign a string to the location property of an object,
JavaScript creates a location object and assigns that string to its
href property.
---
That is not correct for *any* object. The following gives me "string":
---
var x = {}; // or var x=document.body;
x.location = "foo";
typeof x.location
---
So, it is only for window objects, not any object.

Quote:
As a curiosity, in Opera 7 the location.valueOf method is the same
as the location.toString.

Same in Mozilla/5.0 and IE 6.0 SP-1, as expected.
In my IE 6, the location object doesn't have a valueOf property.
What I mean is that in Opera is the *exact* same function, it doesn't
just give the same result. That is:
location.toString == location.valueOf
and
location.valueOf.toString()
gives
---
function toString() {
[native code]
}
---

/L
[1] I know Netscape 4 and Mozilla have ways to make getters and setters
for properties, but that is not portable Javascript.
--
Lasse Reichstein Nielsen - lrn (AT) hotpop (DOT) com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'


Reply With Quote
  #15  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Location object (was: Formatting strings - more complex!) - 11-07-2003 , 12:11 PM



Lasse Reichstein Nielsen wrote:

Quote:
Thomas 'PointedEars' Lahn <PointedEars (AT) web (DOT) de> writes:
In contrast, in older UAs like Opera 6 (IIRC), `location' stores
only a primitive string value.

YRI (You Remember Incorrectly
Having just installed Opera 6.0 again [1], I realize that you are both
right *and* wrong! :-)

I remembered correctly, *but* my testing method was in fact flawed:
From the fact that Opera's `location' shows no properties when iterating
through them with `for (var p in location)', I falsely concluded that it
has none and is therefore no object at all.[2] But as you wrote, it is of
type `object' and has at least `href', `search' and `protocol' properties
(when I think about it, Opera won't have distributed this much if it had not.)

Because you cannot rely on what iteration shows, is there a way to get
all properties of an object other than brute force, and if that, how it
is done?

Quote:
In Opera 4, 5 and 6, location is an object.
It is in v6, and most certainly is in v4 and v5.

Quote:
I don't have Opera 3 installed,
See [1].

Quote:
The same goes for Netscape 4 and 3.
ACK

Quote:
There is some magic to the location object, though, since *assigning*
to it will really assign to location.href.

There is no magic involved See
http://devedge.netscape.com/library/.../location.html

Tha qualified as "magic" to me (something that depends on internal
code and cannot be implemented by a Javscript programmer,
OK. I don't consider things described in specifications "magic".

Quote:
It says:
---
If you assign a string to the location property of an object,
JavaScript creates a location object and assigns that string to its
href property.
---
That is not correct for *any* object. The following gives me "string":
---
var x = {}; // or var x=document.body;
x.location = "foo";
typeof x.location
---
So, it is only for window objects, not any object.
It also says:

Quote:
The location object is contained by the window object and is within its
scope. If you refer to a location object without specifying a window, the
location object represents the current location. If you refer to a
location object and specify a window name, as in windowReference.location,
the location object represents the location of the specified window.
and BTW, your `x' is not a Location object.

Quote:
As a curiosity, in Opera 7 the location.valueOf method is the same
as the location.toString.

Same in Mozilla/5.0 and IE 6.0 SP-1, as expected.

In my IE 6, the location object doesn't have a valueOf property.
Oops! Me too. Debug bug. System halted.


PointedEars
___________
[1] <http://arc.opera.com/pub/opera/>
[2] <http://pointedears.de.vu/scripts/test/location.html>



Reply With Quote
  #16  
Old   
Lasse Reichstein Nielsen
 
Posts: n/a

Default Re: Location object (was: Formatting strings - more complex!) - 11-07-2003 , 01:21 PM



Thomas 'PointedEars' Lahn <PointedEars (AT) web (DOT) de> writes:

Quote:
Because you cannot rely on what iteration shows, is there a way to get
all properties of an object other than brute force, and if that, how it
is done?
No. You can only get the enumerable properties. For the rest, you must
do a brute force search (i.e., you can't in practice).

In Opera, only methods are generally enumerable, and not even all of
them. The enumerable properties of O7's location object are:
assign, reload, replace, toString, and valueOf
As you say, in O6, there are no enumerable properties.

Quote:
I don't have Opera 3 installed,

See [1].
I have it, it's just not installed right now

Quote:
It also says:

| The location object is contained by the window object and is within its
| scope. If you refer to a location object without specifying a window, the
| location object represents the current location. If you refer to a
| location object and specify a window name, as in windowReference.location,
| the location object represents the location of the specified window.
That just sounds like normal scope rules when the "current" window object
is also the global object of the execution context. Location objects are
just properties of window objects.

Quote:
and BTW, your `x' is not a Location object.
Nope. But they did say that if you assigned to the location property
of an object, it would create a location object in its place. Apparenty
it is only true for window objects.

/L
--
Lasse Reichstein Nielsen - lrn (AT) hotpop (DOT) com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'


Reply With Quote
  #17  
Old   
Richard Cornford
 
Posts: n/a

Default Re: Location object (was: Formatting strings - more complex!) - 11-07-2003 , 01:58 PM



"Lasse Reichstein Nielsen" <lrn (AT) hotpop (DOT) com> wrote

Quote:
Because you cannot rely on what iteration shows, is there a
way to get all properties of an object other than brute force,
and if that, how it is done?

No. You can only get the enumerable properties. For the rest,
you must do a brute force search (i.e., you can't in practice).
snip

Do you really mean you can't in practice? Certainly it would not be
practical to test every combination of character sequence that could be
a property name (the end on the universe would arrive before the
results), but I don't see a problem with trying each item in a list of
likely property names against an object to see which return
non-undefined values. Then the usefulness of the results only depends on
the quality of the list of likely property names. It wouldn't be quick
but it wouldn't be so slow as to be impractical.

Richard.




Reply With Quote
  #18  
Old   
Lasse Reichstein Nielsen
 
Posts: n/a

Default Re: Location object (was: Formatting strings - more complex!) - 11-07-2003 , 03:15 PM



"Richard Cornford" <Richard (AT) litotes (DOT) demon.co.uk> writes:

Quote:
Do you really mean you can't in practice? Certainly it would not be
practical to test every combination of character sequence that could
be a property name (the end on the universe would arrive before the
results),
Find them all, no. It is indeed the heat death of the Universe that
prevents that.

Quote:
but I don't see a problem with trying each item in a list of likely
property names against an object to see which return non-undefined
values.
Absolutely possible. Later versions of Javascript even has a way
to check whether an object has a property ("foo" in obj).

Quote:
Then the usefulness of the results only depends on the
quality of the list of likely property names. It wouldn't be quick
but it wouldn't be so slow as to be impractical.
If you know what to look for, it is easier to find it

/L
--
Lasse Reichstein Nielsen - lrn (AT) hotpop (DOT) com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'


Reply With Quote
  #19  
Old   
Dr John Stockton
 
Posts: n/a

Default Re: Formatting strings - more complex! - 11-07-2003 , 03:24 PM



JRS: In article <dtrmqv493s8j8lfdh52f4nmsk4vb7b6cm6 (AT) 4ax (DOT) com>, seen in
news:comp.lang.javascript, Nige <uYYYY (AT) ntlworld (DOT) com> posted at Fri, 7
Nov 2003 10:47:29 :-

Quote:
In comp.lang.javascript, Thomas 'PointedEars' Lahn wrote:

Time zones don't enter into it, all users are in the UK.
I wonder how you can be sure.

Because it is a campaign site for broadband in Kent.
I am reliably informed that there is a modicum of Kent that keeps Paris
Time. However, they are probably not possible direct customers for the
broadband service. And if they were, they might be customers best
avoided (on mere pragmatic grounds; nothing uncomplimentary).

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.