Sunday, May 4, 2008

JavaScript Tip: Submit Form On Enter Key

A common practice with search forms is to have them submit when the enter key is pressed, instead of requiring the user to use their mouse to press the submit button (or using tab key to tab to the submit button).

Here is a simple way to do that in JavaScript:
First, define a input box:


<input name="txtsearchentries" id="txtsearchentries" type="text" size="13" value="" />


Second, create a function to be placed in the head area of the webpage:
//see if the user pressed the enter key while using a specific control

function submitSearchOnEnter(variable)
{
var keyCode;

//get whatever key was just pressed
//try to account for different methods of getting
//event keys depending on implementation
if(window.event)
{
keyCode = window.event.keyCode;
}
else if(variable)
{
keyCode = variable.which;
}

//if the key was the enter key, perform the necessary function
if(keyCode == 13) //13 = enter key
{
//here is where you process the form item or call a function to do that
searchEntries(); //call search
}
}


Third, you need to assign the function above to to be an event handler:

//assign an event handler to the search box so we can have it
//submit when user presses enter
document.getElementById('txtsearchentries').onkeyup = submitSearchOnEnter;

That's it. Should be able to assign it similarly to other fields on the page too, but then you would need to think of how to create separate functions to handle each.

Thursday, May 1, 2008

Work and Work

Work has been frustrating me lately. I work with some people who have big ideas and not that much technical knowledge. Well that isn't the problem more then I have been working on things for multiple departments (outside of my core area) and they seem to all keep coming up with ideas for their respective projects. My boss is a nice guy and likes to please everyone, but that is pushing me towards feature creep hell. Ok, I'm exaggerating, but if you have ever had a job in software development you know the feeling.

On a side note, I've been thinking more about the idea of my own software/web related business. I started looking into Code Igniter (codeigniter.com), which is an open source php framework that should help me save time in personal projects. Sure, I have a multitude of ideas, but getting that effort after work or on the weekends to actually accomplish something is difficult.

頑張るよ as they say in Japan (will try my best!).

Thursday, April 10, 2008

Objects in Javascript

Javascript does not use a standard class model. It uses objects that are like associative array structures of data, or so I have read. Regardless of how it works, you can make a class-ish type construct in Javascript.

I'll try to go over the basics here to help anyone who is interested.

Objects are defined by creating a new function. Inside the function you can define variables and methods that are attached to the primary function. The primary function is really like the constructor for the object as well.

For example:


function ObjectExample(constructorParameter1, constructorParameter2)
{
//define a variable and assign a constructor value to it
this.variable1 = constructorParameter1;
this.variable2 = constructorParameter2;

//you can also define variables with var, but they act differently
var variable3 = this; //assigns an instance pointer to a "constructor" variable

//defines a function that is attached to the object
//this function has one parameter
//pretend that this.variable1 is a reference to a div element
this.statusIndicator = function(statusText)
{
//make sure an element reference was returned before trying to set properties
if(this.variable1 != null)
{
document.getElementById(this.variable1).innerHTML = statusText;
}
}

//you can also have a function with zero parameters
this.aBoringFunction = function()
{
alert('YO!');
}

//you can call an instance function from inside the "constructor" (aka. primary function)
this.aBoringFunction(); //every time an instance is created we will get a pop-up
}



To use the object we just defined we would do something like this:

//create a new instance
var newObjectInstance = ObjectExample('7', divReference);

//execute a object function
newObjectInstance.statusIndicator('Hello...');




That's about it, let me know if I missed something, or there was an error. I typed the code out rather then copying something I know works for readability reasons.

Javascript Rant

I've been working on something recently where I decided to have most of the program run in a client's browser. It's basically a blog system that works with that asp- xml-to-access-db class I released on here as an LGPL piece of code. The clients browser sends/requests information in XML to the server script and everything turned out peachy. I'm overall extremely happy with it.

Now that I'm done writing the script. I learned quite a bit more about Javascript that I didn't know in the past. The initial few days of working with it were tragic. I'm not too fond of Javascript's "object" model and lack of a standard class model. While javascript works and provides a good deal of functionality, it feels "dirty." Maybe my fellow "old school" programmers can understand.

Besides the object model, the DOM in general is a pain to work with, and hard to find information about (that works in multiple browsers). Inconsistencies, things that logically should work don't, and, trying ~5 different suggested methods to do something, but finding out that only 1/ 5 seems to actually work.

If you don't agree with me, have you ever tried encapsulating XMLHttpRequest in a "class" object? There are a few gotchas that I stumbled on, which I hope to talk about in the future.

My tip of the day about Javascript: Objects are functions are objects can be variables with no data hiding what-so-ever...

Saturday, March 29, 2008

Typos!

I was just looking through my blog. I noticed quite a few typos and some terrible English. Ugh. Fixed what I saw, but I imagine there could be quite a bit more disbursed throughout the older posts.

I must have been writing some of these posts in a daze or something. Honestly, I am a native English speaker if you thought otherwise!

My educated guess as to the problem:
- My interest in Japanese is screwing up my language thought process a bit.
- The library of programming syntaxes I try to force into my memory.
- Not proofreading my posts 3+ times before posting them.

Thursday, March 27, 2008

ASP VBScript Database Accessor Class

I've got some limitations with a web server I've been working with. It doesn't have support for .net assemblies, so I can't write web applications in Visual Studio 2005. This leads me to think of creative ways to handle functionality.

Anyways, I came up with an idea of creating a class in VBScript (ASP), where I could send post data to a script on the server that would in-turn access the database file and do what it needs to do. I took it one step further, where all talk between client and server is XML. So the client sends "mock" SQL in the from of XML and in turn receives a response in XML saying if it worked, or returning data from the database.

I'm releasing the code under the Lesser GPL here (revised 04/09/2008):
http://www.netnobi.com/software/lgplaccessdatabase.php

I fixed quite a few errors that I ran into while using the class recently. I also added a few features to the select type xml that can processed by the class.

Saturday, March 1, 2008

DLL Files And .NET ClickOnce Deployment

I want to deploy one of my .NET apps as a ClickOnce application. The issue is that I am connecting to Oracle (see previous posts here and here). Connecting to Oracle requires at least, 4 DLL files that generally have to be in the same directory as the EXE file. The issue is that when the program is published, the DLLs are not referenced in any way, so the program won't work.

Then I read about adding the files to the project, so that ClickOnce and the Publish processor will figure out that the DLLs are required and add them to the manifest.

Here is the process in Visual Studio 2005:
1. Put the 4 DLL files in their own directory in your solution directory (for ease of use mostly).
2. Add all four files to your project by going to "Project->Add Existing Item..."
3. Click on each DLL file in the solution explorer and then change their property: "Copy To Output Directory" to "Copy Always".

That's it! Now when I publish or even run the application I don't have to worry about if the DLL files are where they should be.