TextAreas, AutoComplete Dropdown Box, JScript Garbage Collection, Caching, and More
Edited by Nancy Michell
Download the code for this article:Web0105.exe (35KB)
Browse the code for this article at Code Center:AutoComplete Listbox

Q I'm trying to size a TextArea element. Is there a property you can set to control the width of a TextArea to be the size of its container, in this case a <TD>? Otherwise, is there a way to do this with DHTML?

A
Setting the width of the TextArea to 100 percent will keep it the same size as the table cell it is in. The following code shows how it's done:

  <TABLE width="100%">
  
<TR>
<TD><TEXTAREA style="width: 100%">I am in a table</TEXTAREA>
</TD>
<TD width="130px">I am just to the right of the TEXTAREA</TD>
</TR>
</TABLE>
<TEXTAREA style="width: 100%">I am not!</TEXTAREA>

 

Q In a Web page, is there any way to implement an edit box with a dropdown list that will AutoComplete the text entered with items in the dropdown?

A
Sounds like you've got some DHTML programming on your hands. My suggestion is turn it into a behavior so others can reuse it. The basic strategy is to have a little keyboard buffer containing what the user has typed and then walk through the items in the listbox looking for a near match. The keyboard buffer is automatically cleared after a short interval, so that the user can start over with a new string. By default, the timeout is .75 seconds (250 milliseconds × 3 timeout countdowns)â€"that is, if the user hesitates longer than .75 seconds, the buffer is cleared and whatever the user types in next starts a new string. The timeout is accomplished using the setInterval function of the window object, a standard way in JScript® to yield script execution.

Figure 2 The Listbox in Action
Figure 2 The Listbox in Action

      Figure 1 shows one way to build a listbox so that when the user types some letters, the listbox jumps to the closest match. Figure 2 shows the code in action.

Q
Can someone point me to some good documentation for garbage collection in JScript? I need to find out how JScript cleans up.

A
JScript uses a mark-and-sweep garbage collector with a variety of heuristics used to determine when to run garbage collection. The JScript garbage collector works like this:

  1. When the script engine is shut down, garbage is collected.
  2. When 256 variants, or more than 64KB of strings, or more than 4096 array slots have been allocated, the garbage collector sets a flag that says collect soon.
  3. Whenever a new statement is executed or the script debugger starts, that flag is checked, and if it is set, a collection is done.

      There is an undocumented JScript function called CollectGarbage that forces a garbage collection. This is for testing purposes onlyâ€"do not ship code that calls this function. It is a poor programming practice to write code in JScript that depends on garbage collections being done at particular times. If you need predictable garbage collection, use a language that supports it (like Visual Basic® or VBScript). Note that all of this is the implementation detail of the engine and should not be relied upon because it may change in the future. Note also that the version of JScript supported by Microsoft® .NET will use the .NET Framework garbage collector, a multigenerational mark-and-sweep collector.
      And remember, if you want a deterministic-lifetime app, use a deterministic-lifetime language like C++, Visual Basic 6.0, or VBScript; not an indeterministic-lifetime language like JScript, Scheme, or Java. If you're writing a program that depends on being able to have a deterministic object lifetime, JScript is not the right tool for the job. Trying to make it a deterministic-lifetime language will just create headaches down the road.

Q
I have three questions about writing HTML and optimizing Web development. What is the syntax for the meta tag that makes the browser send images to the cache? What meta tag makes the page always expire? I have some pages with a lot of client-script code and I want this code to be cached, too. If my scripts are in the form

  <SCRIPT src="file1.js"></SCRIPT>
  

 

will file1.js be cached? If not, how can I avoid subsequent downloads of that file?

A
The meta tag that forces the page to expire immediately is

  <META HTTP-EQUIV="Expires" CONTENT="0">
  

 

There are no specific tags that control the caching of images or any other item in particular. By setting the Expires tag, the browser will check for a new version of all the files every time the page is loaded.
      Standard browser behavior is to cache everything unless told to do otherwise on either the server or the client. In the event that you want to prevent caching, use this:

  <meta http-equiv="pragma" content="no-cache">
  

 

Q Call me weird, but I've got an ASP/ADO project that uses JScript as the language the ASP is written in. I need something similar to the VBScript on error resume next. I can test for a recordset.EOF, but I need more than that. I need to gracefully trap errors when the server running SQL Serverâ„¢ goes off-line (ADO returns 'Specified SQL server not found'). I would also like to know if there is a graceful way to trap all ADO errors and tell the client something nice like, "There is a problem, but we know about it. Please don't panic."

A
You need a try/catch block. Here is a rough example.

  rs = new ActiveXObject("ADODB.Recordset");
  
strSQL = "myStoredProc"
try {
rs.Open(strSQL, Session("sDSN"),3);
}
catch(e)
{
if (e instanceof Error)
{
Response.Write(e.description);
Response.End();
}
}

 

Got a question? Send questions and comments to webqa@microsoft.com.

Thanks to the following Microsoft developers for their technical expertise: Doug Anderson, Matthew Barbour, Raymond Chen, John Jansen, Scott Jordan, Praveen Kosuri, Eric Lippert, Mauricio Lorenzon, Michael Pizzo, Mike Pope, Eric Schurman, Chris Sires, Tushar K. Sukale, Adam Vandenberg.

From the May 2001 issue of MSDN Magazine