Javascript Topics








Back to top
Bar and Restaurant Guide
email ... js99@rocket99.com

Copyright 1998-2000 © Citisoft, Inc. All Rights Reserved.

Accessing the Cookies File

For Netscape users, there is a file in the c:\netscape directory called cookies.txt. Explorer uses a directory to store this data, in c:\winnnt\cookies or c:\windows\cookies. Many web pages use this file to store information to store session variables, or to determine if a user is new to a web site.

The file is handled similar to the way an INI file is accessed, except that cookie data has an "expiration date".

All operations to the cookies file are not written to the hard disk until the browser is closed. This is transparent to any functions that use the cookies file.

Each element in the file has a keyword, expiration date, and value. To set a value in the file, a very specific format is needed. Getting a value from this file involves parsing out each line until the desired element is found.

A set of functions is available for accessing the cookies file.

f_setcookie(keyword,value,exp_date)

Sets a value in the cookies file for the lable specified. Null can be used in place of the expiration date if the access is session-specific.
Example: f_setcookie("username","jake",null);

f_getcookie(keyword)

Returns the value from the cookies file for the value specified.
Example: cur_name = f_getcookie("username");

Here is the source code for the functions:

function f_getcookieval(offset) {
        var endstr = document.cookie.indexOf (";", offset);
        if (endstr == -1) endstr = document.cookie.length;
        return unescape(document.cookie.substring(offset, endstr));
}

function f_getcookie(name) {
        var arg = name + "=";
        var alen = arg.length;
        var clen = document.cookie.length;
        var i = 0;
        while (i < clen) {
                var j = i + alen;
                if (document.cookie.substring(i, j) == arg) return f_getcookieval (j);
                i = document.cookie.indexOf(" ", i) + 1;
                if (i == 0) break;
        }
        return null;
}

function f_setcookie (name, value, expires) {
        document.cookie = name + "=" + escape (value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString()));
}

Back to Top

Include Files

Using include files makes code maintenance much easier. Shared libraries can be stored in central locations for multiple applications to access. To create an include file, simple place the function definitions in a text file, and name it with a ".js" extension. The program that uses the file refers to it with the script directive. An example follows.

Here is the main file, test1.htm:

<html>
<head>
<title>ttt</title>
<script src="test2.js">
</script>
</head>
<form>
<input type="button" value="Test Fn" onclick="f_test1()">
</form>
</html>

Here is the include file, which contains a function:

function f_test1()
{
  alert("Test1 function")
}

When the button is pressed, the included function is called.

Back to Top

Pure Virtual Call Error

This error occurs when working with multiple browser windows within a javascript application. It seems to occur when the communication mechanism between the open windows fails, during a close operation. When an object containing a close call fails to work immediately, the "pure virtual call" GPF will occur if the object is clicked multiple times. If the object is clicked just once, the close operation will complete normally.

Two solutions to this problem are:


In the clicked event of the link, disable its address. This will nullify the effect of subsequent clicks.
function f_exit()
{
  document.links[4].href="";
  parent.close();
}
Destroy the current frame within the clicked event. This can be done by performing a write to the document, and closing it.
function f_exit()
{
  document.write("Please wait ...");
  document.close();
  parent.close();
}

Back to Top

What's in a Name?


With all of the different levels of addressing in javascript, object names are very important in keeping things organized. In the figure above, a window contains a frameset which contains three frames. Two of the frames contain forms. One form has two "single line edit" style controls, and another contains three "command buttons". To access the control sle_firstname from fra_tttedit3, the syntax is as follows:
parent.fra_tttedit2.document.frm_tttedit2.sle_firstname.value="Sally";
Note that the naming convention allows each level to be identified easily.

To access a function called f_checkname in the top frame from the bottom frame, the syntax is:

  fra_tttedit2.f_checkname();
Note that the form designation is not needed, since functions exist at the window level.

Back to Top

Setting Focus to a Control During Load

Setting focus to a default control allows the user to begin typing without having to use the mouse. In the following example, a listbox named "lb_cat" exists on form "frm_tttnew". The goal is to set focus to this control after the form is loaded. Here's how the end of the HTML file would look.

<script language="javascript">
<!--
//--------------------------------------------------------------------------
  document.frm_tttnew2.lb_cat.focus()
//--------------------------------------------------------------------------
// -->
</script>
</body>
</html>
Note that this code is after the form definition, and before the last HTML tag.

Back to Top

Passing Variables to Windows

Passing variables in javascript works very similar to the PowerBuilder method. When a window is opened, a variable is assigned to it. Using dot notation, a variable within the window can be assigned a value. Note that the variable does not have to be declared within the calling window; it is created at runtime, just like all other javascript variables. Here is an example:

 w_lookup = window.open("tttlu.html","w_tttlu",
    "toolbar=0,menubar=0,location=0,status=0,height=470,width=630") ;

 w_lookup.w_tttnew = parent;  /* allow window to refer to this window */
 w_lookup.asset_id = 12345;   /* int value  */
 w_lookup.tnt_code = "NTRS";  /* char value */
These three variables can be accessed within the window w_lookup, as follows:
 newvar   = asset_id ;
 w_tttnew.f_processcode(tnt_code);
Note how the calling window's function is called.

Back to Top

Executing Dynamic Code

Javascript statement can be created and executed using the eval function. This is similar to dBase's macro prefix. The eval function takes an argument as a string- this string can be a line of code, or a variable, or any piece of Javascript that could execute in the given context. Here is an example:

var myvar1=0
var myvar2=0
var myvar3=0

/* set all variables to 100 */
for (i=1;i<4;i++) {
          eval("myvar" + i + "=100");
                  }
Here is another example, in which a function calls one of four different functions based on the parameter:
function f_sort(parm1)
{
eval("array1.sort(f_compare" + parm1 + ");");
eval("prev1=loc" + parm1 + "1;");
eval("prev1=loc" + parm1 + "2;");
}
Using the eval function saved 9 lines of code in this case!.

Back to Top

Text Field Entry Issue

When a user types into a text field and presses enter, different behaviors have been observed. When there is one text field on the form, the data disappears; when there are two fields on the form, the data stays in the fields (the expected behavior).

There are many instances when the typed in data needs to be processed by the form before sending to the server; this issue definitely impacts such cases.

Try the pages linked below, and note the odd behavior.

Form with one text field
Form with two text fields





Call a CGI Counter

Simple example depiciting use of a CGI counter.


Number of Hits:








Back to top
Bar and Restaurant Guide
email ... js99@rocket99.com

Copyright 1998-2000 © Citisoft, Inc. All Rights Reserved.