Thursday, October 4, 2007

Firefox object member size limit

I encountered an issue in Firefox with my data coming back from AJAX. The object members of the XML Doc were being truncated to 4096 bytes. If this happens, you can use the function normalize(); on the object resulting from the parser.parseFromString call to have Firefox correct this. Make sure to do a browser check, so you don't get a script error in other browsers that don't have teh normalize function. IE 7 does not have normalize(), but it does not need it in this case because it does not break up the nodes into 4K chunks when you create the document from teh XML stream.

To see what's going on, we can look at the W3C spec at http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html.

"When a document is first made available via the DOM, there is only one Text node for each block of text. Users may create adjacent Text nodes that represent the contents of a given element without any intervening markup, but should be aware that there is no way to represent the separations between these nodes in XML or HTML, so they will not (in general) persist between DOM editing sessions. The normalize() method on Element merges any such adjacent Text objects into a single node for each block of text; this is recommended before employing operations that depend on a particular document structure, such as navigation with XPointers."

According to the first line in the quote above, IE 7 has the correct behavior in this regard and not Firefox. When the document is first created, the browser should not be splitting the nodes into chunks. However, the data is still accessible, even when split. It is placed in child nodes. You should be able to access it via the DOM child node functions/properties. This requires extra coding, so it much easier to just use the "normalize()" function.

MySql stored proc delimiters

When creating a stored proc in MySQL (MySQL 5.0+ supports this), unfortunatly you have to workaround delimiter issues. If you just use semicolons to terminate each line as you would in MS SQL Server, it won't work. So, I'm posting a template here to help developers. I'll use $$ as the delimitor, but you can use a different symbol if you like:

DELIMITER $$;
DROP PROCEDURE IF EXISTS `my_db`.`my_proc_name`$$
CREATE PROCEDURE `my_db`.`my_proc_name` ()
BEGIN
declare my_var_A INT;
[some more statements, each terminated by ; ... ];
END$$
DELIMITER ;$$

The first line configures your new symbol ($$) to be statement terminator. This allows us to put semicolons, the ordinary statement termintor inside our stored procedure, without MySQL trying to go ahead and start executing them as statements when we run the above script to create the stored procedure. See the line "END$$". We terminate that with our new "real" delimiter to allow MySQL to execute that block, thereby creating the whole stored procedure. Then we restore the delimiter to semicolon. Now when we execute our stored procedure, we should be using the default semicolon delimiter, and so the stored procedure will run fine, having ordinary statement terminators.

Wednesday, July 11, 2007

Firefox comments issue

Here's an issue to be aware of in Firefox browsers:

If you make a comment in a .jsp file like this:
<!-- This is my comment -- no real info though -->

It will work fine in IE, but in Firefox I have found this to cause major problems, as it will interpret this as an unterminated comment:
<!-- This is my comment

So, be careful not to include -- in your Firefox comments!

Also, don't use the single tag element terminating slash for script includes:
<script type="text/javascript" src="test.js"></script> (This will break in IE7 or Firefox)
<script type="text/javascript" src="test.js"/> (This will work)

Thursday, June 21, 2007

Javascript URL encoding

If you are using AJAX or something else to communicate via url, you can use the javascript function "escape()" to encode parts of the URL. I list some of the characters that are encoded and some that are not:

Encoded: ! # $ % ^ ( ) = [ ] { } ; " \ ' ? / ,
Not encoded: underscore @ & * - + .

You can also use the javascript function "encodeURI". It has a different set of encoded characters:

Encoded: ` ^ [ ] { } " < > \
Not encoded: underscore tilde ! @ # & $ * ( ) = + , . : ; ' / ?

Wednesday, May 9, 2007

Doctype and browser compatibility

I recommend setting a doctype in your web pages. If you specify the right doctype, then you can avoid IE dropping into "quirks mode" ("quirks mode" = IE backwords compatibility mode). Thus, your pages will look almost the same in IE and Firefox.

You can get a list of doctypes here:
http://www.w3.org/QA/2002/04/valid-dtd-list.html

If you specify an "HTML" doctype, and use DHTML, then your jsp pages may still be in quirks mode. I recommend using an XHTML doctype to solve this problem, like this doctype for example:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"

Wednesday, May 2, 2007

SQL: Adding foreign key relationships

Tip on adding foreign key relationships in SQL:

You can establish a foreign key relation in your create table or with an alter table statment, like this:

ALTER TABLE [table1_name]
ADD CONSTRAINT [constraint_name]
FOREIGN KEY ([field(s) from table1])
REFERENCES [table2_name] ([field(s) from table2])
ON [DELETE CASCADE, or UPDATE, or other command];

(BTW, in the example above, for a cascade, generally table1 would be the child table, and table2 would be the parent table)

If you get an error like this:
Error Code : 1005
Can't create table '.\test\#sql-ac_195.frm' (errno: 150)

it may be the case that your referenced field is not guaranteed unique, which it must be. If you don't have a single unique field in that table, you can use multiple fields to specify the unique key.

Tuesday, May 1, 2007

Establishing primary key auto creates index

In MySQL, you can have a primary key constraint on one or more columns. Establishing a primary key causes MySQL to automatically make a unique key that consists of the columns specified in the primary key. You can verify the existence of this key by running the command:

SHOW INDEX FROM [table name];

In the resulting output, the column 'Non_unique' shows '0' if the column is unique, and '1' if the column is not unique.

SQLyog

I want to recommend the software I use for managing my MySQL databases. It is called SQLyog. You can find it online for download. It is free, although you can buy upgraded versions. It is great for running queries, manipulating data, imports, exports, and you can even manage indexes/triggers/etc.

Wednesday, April 18, 2007

Javascript and Java parseInt

Make sure to specify the radix (base) when using parseInt to convert from string to numerical values. Otherwise if your number is prefixed with zero, a non decimal base is assumed!

In Java:
n = Integer.parseInt(s,10);

In Javascript:
n = parseInt(s,10);

From w3Scholls.com Javascript reference website:

If the radix parameter is omitted, JavaScript assumes the following:

  • If the string begins with "0x", the radix is 16 (hexadecimal)
  • If the string begins with "0", the radix is 8 (octal). This feature is deprecated
  • If the string begins with any other value, the radix is 10 (decimal)
So, you can see how important it is to specify the radix in the case where you are parsing date strings like 08-08-2007. Without specifying the radix, you'd end up with Oct 10, 2007!

Java string comparison tip

Today's tip is just a simple reminder to always use th "equals" function when comparting Strings in Java and not the comparison operator "==".

For example:

String a="a";

//Good
if (a.equals("a"))
fnA();

// Bad
if (a=="a") fnA();

The comparsion operator will compare the value of the String pointer which is probably not what is desired. Generally, you should use the equals function to compare the contents of teh string.

Tuesday, April 17, 2007

MySQL pagination

I'm writing some pagination code for my application. Turns out MySQL has a great method for accomplishing this.

In the select statement, just end it with
LIMIT [offset,] number_of_rows

Combine this with distinct ordering via "ORDER BY" on a unique indexed key, and you have the database doing the pagination without even having to keep open a connection.

Exceeding the 65535 bytes limit

Quite a while ago, I was working on my server-side Java & JSP application, when I encountered this error:

org.apache.jasper.JasperException: Unable to
compile class for JSP
Generated servlet error:

The code of method
_jspx_meth_html_html_0(javax.servlet.jsp.PageContext) is exceeding the 65535
bytes limit



Last time I hit a code size limit was 14 years ago when I was compiling computer programs in Borland Turbo C. Back then, I found the solution was to split the source files.

In this case, I was using the Struts framework, Eclipse/MyEclipse and trying to load the app with Apache Tomcat 5.5.9. What was happening is all the jsp files are being server-side compiled into too large a file. It won't help then to split the files into more jsp files, as these are all compiled in on the server side.

To solve the problem, I broke out all the javascript functions in the jsps into Javascript (.js) files. These cannot access server data, but they don't count towards the size limit as they are not compiled into the server. This worked; I haven't had any more such problems.

Be sure to put anything that does not have a jsp or struts tag into a JS file and not a JSP file. This will help your application performance and avoid file size caps.

You can also put the jsp content in an html file and use an html include:
<!--#include virtual="my_file.html" -->
or
<!--#include file="my_file.html" -->

Monday, April 16, 2007

HTML HREF and onclick javascript functions

If you have an HTML HREF element just to use the onclick handler to launch a javascript function, make sure to add a "return false;" at the end of the onclick handler to prevent unwanted navigation. If the "return false;" is not specified, then after the handler is executed, the default action will be run, which is to navigate to the location specified in the "href=".

For example:

<a href="#" onclick="myfunction(); return false;">click me </a>

Alternatively, you could write the above line without the onclick mentioned. Then just put an event handler in your Javascript code.

AJAX in IE - thread safe

If you write an AJAX function, be sure to use Microsoft.FreeThreadedXMLDOM rather than Microsoft.XMLDOM if you want to ensure only 1 thread can access your xml objects at a time.

For example, you may want to have this:

x = new ActiveXObject("Microsoft.FreeThreadedXMLDOM");

rather than this:

x = new ActiveXObject("Microsoft.XMLDOM");

-Dan Burke

Welcome

Welcome to my blog. This is where I'll post useful tidbits of web development information that I believe will be helpful to other programmers.

-Dan Burke