- var myDiv = new Element('div' { 'tabindex': '0' })
- myDiv.observe('keypress', function(e) {
- var code;
- if (!e) var e = window.event;
- if (e.keyCode) code = e.keyCode;
- else if (e.which) code = e.which;
- alert(String.fromCharCode(code));
- });
Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts
2.11.2009
Attaching keyboard events to alternative browser objects
It took me way too long to find out why I could not listen to keyboard events for DOM objects other than the window, form objects, or links. All you have to do is give the div or any other DOM element a tabindex. Below is an example using the prototype framework.
Labels:
GUI,
Javascript,
Prototype
2.08.2009
Saving Javascript Objects to an SQL Database
Why create intermediate files when you can just store everything in a database? With the ability to store information on local databases like SQ lite and SQL servers being given away like candy from every web hosting service why not use their convenience and security for your websites.
I am going to post some code I wrote to directly save javascript objects to an SQLite database using Google gears. It's an example of writing a single method to save any type of object and I think changes the way we think about saving objects. Correct me if I am wrong, but this seems to be the least amount of code from any programming language for saving objects to an SQL database.
This basically just puts a string together based on any objects properties. All you would have to do it make sure there is a table in the database with the same properties and you have yourself an incredibly versitale SQL function.
I am going to post some code I wrote to directly save javascript objects to an SQLite database using Google gears. It's an example of writing a single method to save any type of object and I think changes the way we think about saving objects. Correct me if I am wrong, but this seems to be the least amount of code from any programming language for saving objects to an SQL database.
- function addSQLite(obj, table) {
- query = "INSERT INTO " + table + " VALUES (";
- z = 0;
- for(x in obj) {
- switch (typeof(obj[x])) {
- case 'string':
- if(z>0) query += ",";
- query += "'" + obj[x] + "'"
- break;
- case 'number':
- if(z>0) query += ",";
- query += obj[x];
- break;
- }
- z++;
- }
- query += ")"
- db.execute(query);
- }
This basically just puts a string together based on any objects properties. All you would have to do it make sure there is a table in the database with the same properties and you have yourself an incredibly versitale SQL function.
Labels:
Database Storage,
Javascript,
Programming,
SQL
Subscribe to:
Posts (Atom)