Archive for the 'JavaScript' Category

Rendering N-sided Polygons with DHTML

Tuesday, November 28th, 2006

With this technique, convex polygons with any number of sides can be rendered, with a solid color or a background image as the fill. Click here or the image below to launch the demo (opens in a pop-up window).
 

DHTML Polygon Rendering Demo

You can probably figure it out on your own by playing with the checkboxes at the lower left corner of the pop-up, but read on to find out how it all works. Read the rest of this entry »

PHP-style Serialization of JavaScript Objects

Sunday, July 30th, 2006

Heavily-scripted data entry forms that expand on-the-fly as the user enters new data make life easier for the user. However, it can be hell for a developer to devise a method for the representation and submission of data with arbitrary length and depth.

A logical thing to do seems to be to take an object-oriented approach to represent all client-side data as one or multiple JavaScript objects. The objects themselves could be arrays or objects with child objects and so on. Of course the objects need to be serialized in some way before they’re posted back. Now, if you’re using PHP at the back-end, you might make use one of the multitude of JSON implementations out there for posting back the data in JSON format and converting the data back to PHP objects (assuming that you want to end up with PHP objects.) JSON is pretty much native to JavaScript (clever use of the toSource method may cut it in most cases) but you need extra PHP code for deserialization.

If you want to take the opposite approach and use PHP-native serialization, then the following function will convert any complex JavaScript object into PHP-serialized string which can be converted back into PHP objects with a single call to the deserialize PHP function. Read the rest of this entry »

Finding Out Class Names of JavaScript Objects

Monday, July 10th, 2006

JavaScript lacks a built-in function for getting the exact class names of object instances. The typeof operator just returns “object” for instances of both Object and Array, as well as user-defined classes. The following example illustrates this:

function MyClassA()
{
}

var obj = new Object();
var arr = new Array();
var myobj = new MyClassA();

document.write(typeof(obj) + "<br />");
document.write(typeof(arr) + "<br />");
document.write(typeof(myobj) + "<br />");

The output of the code above, with your browser is:

Read the rest of this entry »