Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / MS JScript / July 2008



Tip: Looking for answers? Try searching our database.

Application objects

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Carlos Felipe França da Fonseca - 05 Jul 2008 10:02 GMT
My global.asa initiates the following application object:

<SCRIPT LANGUAGE=VBScript RUNAT=Server>
Sub Application_OnStart
   Application("TranslateDomain") = "MSFT.com"
End Sub

A different file from the same website uses JavaScript and needs to call
that application object TranslateDomain.
How is the syntax to call that application object in Javascript?

Thanks!!!

Felipe
Joe Fawcett - 05 Jul 2008 10:43 GMT
The Application object has a collection, Contents, which is what you are
accessing via Application("TranslateDomain") .
So I think in JScript you would use Application["TranslateDomain"] or
Application.Contents("TranslateDomain")
Should be easy enough to set up a one page asp web site and try a few
variations.

That's assuming that when you were talking about server-side script. If you
meant how to retrieve the value client-side then you'll need something like:
<script type="text/javascript">
var sTranslateDomain = "<% = Application("TranslateDomain") %>";
</script>

Signature

Joe Fawcett (MVP - XML)
http://joe.fawcett.name

> My global.asa initiates the following application object:
>
[quoted text clipped - 10 lines]
>
> Felipe
Evertjan. - 05 Jul 2008 11:03 GMT
Joe Fawcett wrote on 05 jul 2008 in microsoft.public.scripting.jscript:

> The Application object has a collection, Contents, which is what you are
> accessing via Application("TranslateDomain") .
> So I think in JScript you would use Application["TranslateDomain"] or
> Application.Contents("TranslateDomain")
> Should be easy enough to set up a one page asp web site and try a few
> variations.

Repeatingly run:

============= test.asp =============

<script language='javascript' runat='server'>

Response.write(Application('test'));
Application('test') = Application('test') + 'a';

</script>

====================================

Application.Contents and Application.contents give an error.

Signature

Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Carlos Felipe França da Fonseca - 05 Jul 2008 22:59 GMT
Evertjan,

Do I need to use simple quotes or double quotes?

Tks,

Felipe

> Joe Fawcett wrote on 05 jul 2008 in microsoft.public.scripting.jscript:
>
[quoted text clipped - 19 lines]
>
> Application.Contents and Application.contents give an error.
Anthony Jones - 06 Jul 2008 08:25 GMT
> Evertjan,
>
> Do I need to use simple quotes or double quotes?

You can enclose a string either like this  'a string' or like this "another
string" in javascript.  IOW it doesn't matter you use either.

Signature

Anthony Jones - MVP ASP/ASP.NET

Anthony Jones - 06 Jul 2008 08:23 GMT
> Joe Fawcett wrote on 05 jul 2008 in microsoft.public.scripting.jscript:
>
[quoted text clipped - 19 lines]
>
> Application.Contents and Application.contents give an error.

What error do you get? Works fine for me.

Signature

Anthony Jones - MVP ASP/ASP.NET

Evertjan. - 06 Jul 2008 16:13 GMT
Anthony Jones wrote on 06 jul 2008 in
microsoft.public.scripting.jscript:

>> Joe Fawcett wrote on 05 jul 2008 in
>> microsoft.public.scripting.jscript:
[quoted text clipped - 22 lines]
>
> What error do you get? Works fine for me.

Same here.

Prehaps I made a spelling mistake.

Signature

Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Carlos Felipe França da Fonseca - 07 Jul 2008 00:15 GMT
Hang on guys...

What's the syntax for Application.Contents?

Application["TranslateDomain"]
or
Application.Contents("TranslateDomain")
or
Application.Contents["TranslateDomain"]

Thanks,

Felipe

> Anthony Jones wrote on 06 jul 2008 in
> microsoft.public.scripting.jscript:
[quoted text clipped - 29 lines]
>
> Prehaps I made a spelling mistake.
Joe Fawcett - 07 Jul 2008 08:00 GMT
> Hang on guys...
>
[quoted text clipped - 9 lines]
>
> Felipe

I believe it's:
Application.Contents("TranslateDomain")

Look up "empirical" :)

Signature

Joe Fawcett (MVP - XML)

http://joe.fawcett.name

Evertjan. - 07 Jul 2008 09:48 GMT
Joe Fawcett wrote on 07 jul 2008 in microsoft.public.scripting.jscript:

>> Hang on guys...
>>
[quoted text clipped - 10 lines]
>
> Look up "empirical" :)

A collection is not a jscript object, methinks.

Collections define their elements inside (),
objects define their elements inside [],
arrays also define their elements inside [].

Collection of ids:
 getElementById()

Object existing of a single element:
 getElementById('myIdea')

Collection of TagNames:
 getElementsByTagName()

Object existing of all divs:
 getElementsByTagName('div')

A single element of that object,
in itself also an object:
 getElementsByTagName('div')[1]

Signature

Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Joe Fawcett - 07 Jul 2008 16:50 GMT
A five minute experiment gave me this:

<%@ language="JScript" %>
<%

 Application.Contents("One") = 1;
 Application("Two") = 2;
 var enumApp = new Enumerator(Application.Contents);
 for (; !enumApp.atEnd(); enumApp.moveNext())
 {
   var oItem = enumApp.item();
   Response.Write(oItem + ": " + Application(oItem) + "<br>");
 }
 Response.Write(Application.Contents("One") + "<br>");
 Response.Write(Application("Two") + "<br>");

%>

Not sure why that was so hard, the output was:

One: 1
Two: 2
1
2

Signature

Joe Fawcett (MVP - XML)

http://joe.fawcett.name

> Joe Fawcett wrote on 07 jul 2008 in microsoft.public.scripting.jscript:
>
[quoted text clipped - 34 lines]
> in itself also an object:
>  getElementsByTagName('div')[1]
Anthony Jones - 08 Jul 2008 09:28 GMT
> Joe Fawcett wrote on 07 jul 2008 in microsoft.public.scripting.jscript:
>
[quoted text clipped - 21 lines]
> Collection of ids:
>   getElementById()

No getElementById is a function.  Creating a mental model that this is a
collection is IMO detrimental to a clear understanding.

> Object existing of a single element:
>   getElementById('myIdea')

Calling the function returns a value.  The functions parameter specifies a
key which the function uses internally to select a value to return.

> Collection of TagNames:
>   getElementsByTagName()

getElementsByTagName is also function.

> Object existing of all divs:
>   getElementsByTagName('div')

Return an array of values, in this case the values are HTML Div Element
objects

> A single element of that object,
> in itself also an object:
>   getElementsByTagName('div')[1]

Get an array of HTML Div Element objects then use an array indexer [1] to
retrieve the second entry in the array.

Signature

Anthony Jones - MVP ASP/ASP.NET

Evertjan. - 08 Jul 2008 11:11 GMT
Anthony Jones wrote on 08 jul 2008 in
microsoft.public.scripting.jscript:

>> Collection of ids:
>>   getElementById()
>
> No getElementById is a function.  Creating a mental model that this is
> a collection is IMO detrimental to a clear understanding.

You could also call getElementById a string.

It calls a single element of the collection of ids in the document or in
part of that.

>> Object existing of a single element:
>>   getElementById('myIdea')
>>
> Calling the function returns a value.  The functions parameter
> specifies a key which the function uses internally to select a value
> to return.

That is implementation, if I understand your words.

In common words it returns [the pointer to] the element.

>> Collection of TagNames:
>>   getElementsByTagName()
>
> getElementsByTagName is also function.

You are misreading I did not write "is".

It calls/returns a collection of a specified tagName out of the
collection of tagNames.


>> Object existing of all divs:
>>   getElementsByTagName('div')
>>
> Return an array of values, in this case the values are HTML Div
> Element objects

Values are never objects, they are pointers to objects.

>> A single element of that object,
>> in itself also an object:
>>   getElementsByTagName('div')[1]
>
> Get an array of HTML Div Element objects then use an array indexer [1]
> to retrieve the second entry in the array.

That was the whole point to give some logic to the difference between

xxx() for to get collections in general, a specified function indeed.

and

xxx[] to get an element of an array or an object.

Signature

Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Anthony Jones - 08 Jul 2008 13:56 GMT
> Anthony Jones wrote on 08 jul 2008 in
> microsoft.public.scripting.jscript:
[quoted text clipped - 6 lines]
>
> You could also call getElementById a string.

You could call an elephant a horse if you wanted but I'm not quite sure why
you would want to. ;)

> It calls a single element of the collection of ids in the document or in
> part of that.

It returns the first element in the document that has an Id that matches the
parameter.  It is not productive to think of it any more than that.

> >> Object existing of a single element:
> >>   getElementById('myIdea')
[quoted text clipped - 6 lines]
>
> In common words it returns [the pointer to] the element.

The concept of a pointer is a C++ism that is not needed in JavaScript.  The
behaviour of an object is defined and the fact that a variable is actually a
reference to an object is intrinsic.  Unlike C++ you can't ever hold an
object 'by-value' in variable so there is no need to make such a
distinction.

(I'm having a Deja moment about having this same discussion re: this
distinction before).

> >> Collection of TagNames:
> >>   getElementsByTagName()
[quoted text clipped - 5 lines]
> It calls/returns a collection of a specified tagName out of the
> collection of tagNames.

That is true this function does return a collection object.

> >> Object existing of all divs:
> >>   getElementsByTagName('div')
[quoted text clipped - 3 lines]
>
> Values are never objects, they are pointers to objects.

As stated above such distinction is not really necessary, an "object value"
is implicit a reference to the object.

> >> A single element of that object,
> >> in itself also an object:
[quoted text clipped - 6 lines]
>
> xxx() for to get collections in general, a specified function indeed.

However your list of examples showed one function getting an element and
another function getting a collection (which is an object itself) of
objects. Both are just simply functions.   You said earlier:-

"Collections define their elements inside (), "

They don't.  () simply means call the function on LHS with the parameters
listed.  As I described in another reply in this thread there is some
special handling of () in JScript where the LHS is an ActiveXObject but
neither of your examples are of that type, they are both simply function
calls.

Here is an example of the sort of collection access I think you were
refering to:-

var col = document.getElementsByTagName("TR");
alert(col("someid").innerText);

JScript converts col("someid") to a col.get_item("someid") function call
(item being the default property), of course JavaScript in something like FF
just errors saying col is not a function.

> and
>
> xxx[] to get an element of an array or an object.

Specifically  [ ] means 'of the object supplied on the LHS access the
attribute specified'.

For example to the above example you could add:-

alert(col["length"])  which is equivalent to alert(col.length)

Similarly alert(col["1"].innerText) is equivalent to alert(col[1].innerText)

Also equivalent is alert(col.item(1).innerText) and alert(col(1).innerText)

The COM objects that are returned by MSHTML implement the IDispatchEx which
supports the implementation of [ ] on these objects.

Using [ ] on COM objects that do not implement IDispatchEx will return
undefined or will throw an error.

Signature

Anthony Jones - MVP ASP/ASP.NET

Anthony Jones - 08 Jul 2008 09:58 GMT
> Hang on guys...
>
[quoted text clipped - 5 lines]
> or
> Application.Contents["TranslateDomain"]

Attempting to use [ ] on a COM object such as Application will result in
Method or Property not supported error.

In JavaScript the  [ ]  syntax  allows arbitary named attributes to be added
and accessed on an object.   Standard COM objects such as those in ASP do
not support this.

Application("TranslateDomain") works since JScript co-operates with COM.

x("y") where x is a COM object is handled by JScript by the assumption that
there is a default member defined on x and that his member is a property.
JScript transforms this to method call on x which is the get accessor of the
default property.  In the case of Application the default property is Value
therefore:-

var val = Application("y")

in effect becomes a simple method call:-

var val = Application.get_Value("y")

where x("y") is on the LHS of = it becomes an assignment and the put method
of the default property is used to which the value parameter is passed
result of the RHS expression:-

Application("y") = a + b

in effect becomes another method call:-

Application.put_Value("y", a + b)

The Application.Contents property is an object  whose default property is
.Item.  Application.Contents.Item and Application.Value are equivalent.

Signature

Anthony Jones - MVP ASP/ASP.NET

Old Pedant - 12 Jul 2008 01:33 GMT
> Application("TranslateDomain") works since JScript co-operates with COM.
>
[quoted text clipped - 9 lines]
>
> var val = Application.get_Value("y")

Actually, that's not quite pedantic enough.

In point of fact, the ActiveX interface handler (in common to both JS and
VBS) will first try to see if indeed there *IS* a default *METHOD* property
on the object.  If not, it then checks to see if there is a default property
on the object that is itself an object.

The Application object does *NOT* have any default method property.  It
does, however, have a default OBJECT property named Contents.

So then the call is transformed, automatically, into
   var val = Application.Contents("y")

The default "get" property on the Contents collection (yes, it's a
collection) is Item, so now you are actually invoking:
   var val = Application.Contents.Item("y")

Since JS doesn't have the handy TypeName function, it's easier to demo this
in VBScript code:

******** junk.asp ********
<%
Session("foo") = "bar"

Response.Write "TypeName(Session) is " & TypeName(Session) & "<br/>"
Response.Write "TypeName(Session.Contents) is " & TypeName(Session.Contents)
& "<br/>"
Response.Write "TypeName(Session.Contents.Item(foo)) is " &
TypeName(Session.Contents.Item("foo")) & "<br/>"
Response.Write "TypeName(Session(foo)) is " & TypeName(Session("foo")) &
"<br/>"
%>
******************

The output of that is, not horribly surprisingly:

TypeName(Session) is ISessionObject
TypeName(Session.Contents) is IVariantDictionary
TypeName(Session.Contents.Item(foo)) is String
TypeName(Session(foo)) is String

I used Session so as not to litter up my app space awaiting a restart.  But
honest, they are implemented the same way.

And all of this, of course, is essentially the same thing we see when people
use
   RS.Fields.Item("foo").Value
instead of allowing JS/VS to supply all the defaults and just using
   RS("foo")

Incidentally (or maybe not so incidentally), you *WLL* get MEASURABLY better
performance by using just
  RS("foo")
and letting VBS/JS/COM do all the selection of defaults for you.  Again,
this is because each extra byte code token added to the compiled form of JS
or VBS requires 60 to 100 lines of C++ code to execute, and the
COM-default-property-resolution code is considerably smaller than that.  
Idiot DreamWeaver designers should take note.  Not that they will at this
late date.
Old Pedant - 12 Jul 2008 01:40 GMT
Not likely anybody will read this at this late date, but w.t.h.

The results of doing this on a recordset:

Response.Write "TypeName(RS) is " & TypeName(RS) & "<BR/>"
Response.Write "TypeName(RS.Fields) is " & TypeName(RS.Fields) & "<BR/>"
Response.Write "TypeName(RS.Fields.Item(0)) is " &
TypeName(RS.Fields.Item(0)) & "<BR/>"
Response.Write "TypeName(RS.Fields.Item(0).Value) is " &
TypeName(RS.Fields.Item(0).Value) & "<BR/><HR>"

are these:

TypeName(RS) is Recordset
TypeName(RS.Fields) is Fields
TypeName(RS.Fields.Item(0)) is Field
TypeName(RS.Fields.Item(0).Value) is String

The difference between this and Session or Application object is, of course,
that the elements of the Fields collection are themselves objects, so one
more default property (Value) is needed to get to the data you are commonly
interested in.
Dave Anderson - 14 Jul 2008 13:33 GMT
> TypeName(RS) is Recordset
> TypeName(RS.Fields) is Fields
> TypeName(RS.Fields.Item(0)) is Field
> TypeName(RS.Fields.Item(0).Value) is String

...something that every JScript ASP coder knows by heart.

Signature

Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms.

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.