Mental Jetsam

By Peter Finch

Archive for April, 2007

How to submit a HTML form with the Enter Key

Posted by pcfinch on April 23, 2007

If your form does not have a SUBMIT button then, in some cases the browser will not automatically submit the form when you press ENTER. Here is how to submit the form when the user presses the ENTER key.

The code simply intercepts all the key presses on the page and when the ENTER key is pressed if submits the form by calling the appropriate JavaScript function. In this case submitForm(), however, it could be any other function. This function works on Mozilla and Internet Explorer (IE).

document.onkeypress = processKey;

function processKey(e)
  if (null == e)
    e = window.event ;
  if (e.keyCode == 13)  {
    submitForm() ;

Posted in HTML, javascript | 8 Comments »

Unescape a Python “escaped” string

Posted by pcfinch on April 13, 2007

Python has a very useful regular expression function to escape special characters out a string. Oddly, there is no reverse function. Note that python itself will automatically escape the backslash when printing out the string. e.g..

>>> a = re.escape('Special \\#`1\\')
>>> a
'Special\\ \\\\\\#\\`1'\\\\

A simple way to “unescape” the string is to use a regular expression again. The following RE searches the string to a backslash followed by any character and replaced it with that character. The RE selects the character in to a group (.) and then uses that group in the substitution string 1.

>>> z = re.sub(r'\\(.)', r'\1', a)
>>> z
'Special \\#`1\\'

The tick here is the back reference to the character following the escaping “\”. You may think that all you need to do is replace all the “\” characters in the escaped string with nothing “”. Unfortunately, that doesn’t work correctly when you have an escaped “\” e.g. “\\” (which results in an escaped version “\\\\”) … confused?. Performing a simple replace on “\” will result in empty string.

Posted in Python | 4 Comments »

Accessing an ADO Database table from VB Script

Posted by pcfinch on April 10, 2007

This is a simple bit of VB Script that will access a database via an ADO object and extract some information from a table. Simply create a file called something like “whatsmypasswordtoday.vbs” and you can double-click on it from explorer, or run from the command line, to check your password if you ever forgot it [not that that ever happens].

In this case the code is querying the users table of an Oracle database and extracting the password of the user called “bob”. More advanced versions of this script could be used to extract or insert information into the database and perform routine batch operations. Although you probably would not write an entire application in VB Script (grin), the following is a good example of now useful, quick and powerful VB script can be in automating everyday windows tasks.

Option Explicit
' on error resume next

dim CR, strConnection, oConn, oCmd, rs, record

CR = chr(13)

strConnection = "Driver={Microsoft ODBC for Oracle};;UID=user;Pwd=password;"

Set oConn = CreateObject("ADODB.Connection")
Set oCmd = CreateObject("ADODB.Command")

oConn.Open strConnection
if (Err.Number <> 0) then
  MsgBox "Unable to connect to database" & CR &_
  "Error(" & Err.Number & ")" & CR &_
end if

Set oCmd.ActiveConnection = oConn
set rs = oCmd.Execute

Do Until rs.EOF
  msgBox "password=" & rs.fields("password")

set oCmd = nothing
set oConn = nothing

Posted in VBScript | Leave a Comment »

Finding a form element in a DIV using it’s name in Netscape 4

Posted by pcfinch on April 9, 2007

Netscape 4 is a little more complexity compared with the current (2004+) browser object models, as all HTML form fields that appear in divisions (DIV tags) actually exist in layers. i.e.

<div name="myDiv">
<form method="get" action="doit.jsp" name="myForm">
<input type="text" name="myInput">

A field in a DIV named myDiv will actually exist in the following location. The trick is that the layer object myDiv actually contains a seperate document that can be addressed just like any other document. It is also important to note that Netscape 4 does not allow forms to span divisions, so keep the form and the fields all in the say DIV tag.

obj = document.myDiv.document.myForm.myInput;

The following JavaScript will iterate over all the fields in all the DIV’s (layers) in the document and then iterate over all the forms and fields.

var s = "" ;
for(i = 0; i < document.layers.length; i++)
  s += "L" + i + ":" + document.layers[i].name + "(" ;
  l = document.layers[i] ;
  for(f = 0; f < l.document.forms.length; f++)
    s += "F" + f + ":" + l.document.forms[f].name + "[" ;
    f = l.document.forms[f] ;
    for(e = 0; e < f.elements.length ; e++)
      s += " E" + f + ":" + f.elements[e].name ;
    s += "]"
  s += ")" ;

Posted in HTML, javascript | Leave a Comment »