Quaisitime.com is now released! I have been working on this for a couple years now. Thanks to all my blog readers. Now you can see what I have been using my web programming for. Go to quasitime.com and get your free account (pro and business solutions available as well).
I know I haven't posted for a while as it has been crunch time at my company, but I will resume shortly. I have a number of tips I need to post. I am also considering posting an RFC that, if adopted by operating systems and browsers, will make the world wide web work better.
-Dan
Thursday, May 14, 2009
Sunday, February 24, 2008
Javascript Reference Pointers
Does Javascript assign values by "pointer" (to use the C term)?
Yes, indeed. Let's take a look how this impacts code.
When dealing with Strings you generally don't need to worry about this because the String modification functions don't actually modify the original String, they just return a new one.
Try this code after guessing what the output will be:
// Test 1: Append
var str_a = "aloha";
var str_b = str_a;
alert("str_a==str_b: "+(str_a==str_b));
str_b = "aloh";
str_b += "a";
alert("str_a="+str_a);
alert("str_b="+str_b);
alert("str_a==str_b: "+(str_a==str_b));
// Test 2: toUppercase
str_b = str_a;
str_b.toUpperCase();
alert("str_a="+str_a);
alert("str_b="+str_b);
In the first test appending to str_b does not affect str_a, even though they originally pointed to the same string. The appending actually sets str_b to a new string that contains the appended string. The comparison is overridden though, so your == for string comparisons still works even though the pointer values are different.
In the second test, the toUpperCase() function does not modify either string. It only returns a new string that is uppercase, which we haven't used. So, in place functions will not affect str_a, even if they are used on str_b. Obviously, if we did copy the result to str_b, then str_b would point to something else, and not affect str_a. I haven't tested with the valueOf() function, so if you are using that, you may want to test it to check the pointer behavior.
Next, let's look at the Date Object. What would you expect as a result from this code?
var jsdate_a = new Date();
var jsdate_b = jsdate_a;
alert("a: " +jsdate_a);
alert("b: " +jsdate_b);
jsdate_b.setHours(0);
alert("a: " +jsdate_a);
alert("b: " +jsdate_b);
In this case jsdate_a does get its hours reset to zero by modifying jsdate_b! That is because the Date Class has functions which modify the data in the object itself, without just creating a new instance. Since jsdate_a and jsdate_b point to the same Date Object, a modification to the data pointed to by one of them, affects the data pointed to by the other.
Yes, indeed. Let's take a look how this impacts code.
When dealing with Strings you generally don't need to worry about this because the String modification functions don't actually modify the original String, they just return a new one.
Try this code after guessing what the output will be:
// Test 1: Append
var str_a = "aloha";
var str_b = str_a;
alert("str_a==str_b: "+(str_a==str_b));
str_b = "aloh";
str_b += "a";
alert("str_a="+str_a);
alert("str_b="+str_b);
alert("str_a==str_b: "+(str_a==str_b));
// Test 2: toUppercase
str_b = str_a;
str_b.toUpperCase();
alert("str_a="+str_a);
alert("str_b="+str_b);
In the first test appending to str_b does not affect str_a, even though they originally pointed to the same string. The appending actually sets str_b to a new string that contains the appended string. The comparison is overridden though, so your == for string comparisons still works even though the pointer values are different.
In the second test, the toUpperCase() function does not modify either string. It only returns a new string that is uppercase, which we haven't used. So, in place functions will not affect str_a, even if they are used on str_b. Obviously, if we did copy the result to str_b, then str_b would point to something else, and not affect str_a. I haven't tested with the valueOf() function, so if you are using that, you may want to test it to check the pointer behavior.
Next, let's look at the Date Object. What would you expect as a result from this code?
var jsdate_a = new Date();
var jsdate_b = jsdate_a;
alert("a: " +jsdate_a);
alert("b: " +jsdate_b);
jsdate_b.setHours(0);
alert("a: " +jsdate_a);
alert("b: " +jsdate_b);
In this case jsdate_a does get its hours reset to zero by modifying jsdate_b! That is because the Date Class has functions which modify the data in the object itself, without just creating a new instance. Since jsdate_a and jsdate_b point to the same Date Object, a modification to the data pointed to by one of them, affects the data pointed to by the other.
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.
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.
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)
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 ! @ # & $ * ( ) = + , . : ; ' / ?
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"
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.
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.
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.
Subscribe to:
Posts (Atom)