Archive for the ‘javascript’ Category

So color in KML is in aaBBGGRR!

September 20, 2011

not in aaRRGGBB as you normally expect:

<color>ffFFFF00</color> is cyan

<color>ffff00ff</color> is pink

JavaScript 101: Creating a custom object

December 6, 2009

// initialize the object
var MyOwnClass = {};

// define all the members for the object
(function(){

// declare and define a property
var foo = “hello world! I’m foo.”;

// define a method
this.init = function()
{
alert(“yo! ” + foo);
}

}).apply(MyOwnClass );

// hook up with the DOM event
window.addEventListener(“load”, MyOwnClass.init, false);

Block statements in control flow constructs

December 2, 2009

There is no need to block (using braces to wrap) single line statements in a control flow (as-if, do-while, for/while); and you can mix blocks and non block statements together like this:

if(num > 0)
trace(num+” is positive;\n”);
else if(num <0)
{
trace(num+” is negative.”);
trace(num+” don’t be negative;\n”);
}
else if(num == 0)
trace(num+” is neutral;\n”);

This applies to all the control statements although the example here is just of if/else .

With this being said, The Elements of C# Style recommends Always us block statements in control flow constructs. Reason one is that it makes it easy to add additional statements in it; reason two is it’s easier to read than those without blocks.

Reference content from chrome in Mozilla Firefox

November 28, 2009

This is a snippet of text from Working with windows in chrome code:

In case of <browser type="content-primary" />, you can use the content shortcut property to access the Window object of the content document. You can use content.document in a browser.xul overlay to access the web page in the selected tab in a Firefox window. For example:

//alerts selected text in the main content
alert(content.window.getSelection());
// alerts the title of the document displayed in the content-primary widget
alert(content.document.title);

There is no shortcut for accessing document in the sidebar. You need to use

document.getElementById(“sidebar”).contentDocument

or

.contentWindow

,

like when Accessing content documents

Works in Firefox 3.5.5.

Notes for Building iPhone With HTML, CSS and JavaScript by Jonathan Stark

October 17, 2009

The book. Author: Jonathan Stark.

  • Page scaling in iPhone
    By default Mobile Safari will zoom out for a page that is wider than 980px unless you specify “viewport” <meta> tag to tell it not to;

    <meta name = "viewport" content = "user-scalable=no, width=device-width" />
  • CSS for iPhone
    <link rel=”stylesheet” type=”text/css” href=”styles/iphone.css” media=”only screen and (max-device-width: 480px)” />
    <link rel="stylesheet" type="text/css" href="styles/desktop.css" media="screen and (min-device-width: 481px)" />
    <!--[if IE]>
    <link rel="stylesheet" type="text/css" href="styles/desktop.css" media="all" />

    <![endif]-->
  • Icon for iPhone home screen, a.k.a. “web clip icon”By default, this is a 57px by 57px named “apple-touch-icon.png” in your web root. iPhone will add gloss and 10px(?) radius rounded corner to it. If you don’t want the iPhone to add effects to your webclip icon, change the name of the file to “apple-touch-icon-precomposed.png“.In the case you want to add a web clip icon for an individual page that is different from the rest of the site, add one of the following lines to the of the page:

  • <link rel="apple-touch-icon" href="myCustomIcon.png" />
    <link rel="apple-touch-icon-precomposed" href="myCustomIcon.png" />

  • Full-screen modeAdd the following line to the of your page and your web app will in display full screen mode when launched from the web clip icon, which will give you an extra of 104 px in height:

    <meta name="apple-mobile-web-app-capable" content="yes" />

    Once you’ve added the apple-mobile-web-app-capable meta tag, you have the option to control the background color of the 20 pixel status bar at the top of the screen using the apple-mobile-web-app-status-bar-style meta tag. The normal gray Safari status bar is the default, or you can change it to black. You can also set it to black-translucent which makes it partialy transparent and additionally removes it from the document flow. In other words, your content will be shifted up by 20 pixels and behind the status bar the when page first loads, so you might have to position your header a little lower to compensate.

  • Here are some webkit(?)/Safari specific CSS or newer CSS3 attributes or confusing ones:

  • text-shadow
    text-shadow: 0px 1px 0px #fff;

    The parameters: horizontal offset, vertical offset, blur, and color.
  • -webkit-gradient
    background-image: -webkit-gradient(linear, left top, left bottom, from(#ccc), to(#999));
    a CSS gradient can be used anywhere you would normally specify a url() (e.g. background image, list style image). The parameters from left to right are as follows: the gradient type (can be linear or radial), the starting point of the gradient (can be left top, left bottom, right top, or right bottom), the end point of the gradient, the starting color, and the ending color.
  • -webkit-border-top-left-radius, etc.
    #header ul li:first-child a {
    -webkit-border-top-left-radius: 8px;
    -webkit-border-top-right-radius: 8px;
    }
  • first-child/last-child pseudo classes

    #header ul li:first-child a {
    -webkit-border-top-left-radius: 8px;
    -webkit-border-top-right-radius: 8px;
    }
    #header ul li:last-child a {
    -webkit-border-bottom-left-radius: 8px;
    -webkit-border-bottom-right-radius: 8px;
    }

    The first-child and last-child pseudo classes can be a bit confusing. For example, li:first-child will select the first li that is the child of its ul parent. It does not select whatever is the first child of the li.
  • rgb/rgba
    text-shadow: rgba(125,125,125,0.6) 0px -1px 0px;
  • -webkit-border-image and border-width
    border-width: 0 8px 0 8px;
    -webkit-border-image: url(images/button.png) 0 8 0 8;

    These two propoerties together allow you to assign portions of a single image to the border area of an element. They are similar to 9-slice scaling in Flash or 9-patch in Android. The parameters in border-width refer to top/right/bottom/left side of the image.

JSON to XML

June 1, 2009

A lightweight plugin for Eclipse. Works good for me on Flex Builder 3 (v3.0.1.2x).

JavaScript xml2json and json2xml.

Code snippet: clamp value into a range

April 24, 2009

var myVal:Number = Math.max(USER_MIN,  Math.min(USER_MAX, calculateMyDesiredValue());

Inspired by Ely Greenfield. (Note: “Inspired” is an elegent way of saying “copy”.)

Postfix operators: x++

March 30, 2009

Although postfix operators are unary operators, they are classified separately from the rest of the unary operators because of their higher precedence and special behavior. When a postfix operator is used as part of a larger expression, the expression’s value is returned before the postfix operator is processed. In other unary operators, such as prefix increments/decrements, the increment or decrement operation is completed before the value of the overall expression is returned.

#include <iostream>
using namespace std;

int main()
{
int max = 3;
int total = 7;

int count = 0;
cout<<“unary test for postfix:”<<endl;
for(int i = 0; i< total; i++)
{
count = count >= max? 0 : count++;
cout<<“#”<<i<<“: “<<“count is “<<count<<endl;
}
cout<<endl;

count = 0;
cout<<“unary test for prefix:”<<endl;
for(int i = 0; i< total; i++)
{
count = count >= max? 0 : ++count;
cout<<“#”<<i<<“: “<<“count is “<<count<<endl;
}
cout<<endl;

cout<<“for loop test:”<<endl;
count = 0;
for(int i = 0; i< total; i++)
{
if(count >= max)
{
count = 0;
}else
{
//both are the same.
//count++;
++count;
}
cout<<“#”<<i<<“: “<<“count is “<<count<<endl;
}
cout<<endl;
}
/**result
unary test for postfix:
#0: count is 0
#1: count is 0
#2: count is 0
#3: count is 0
#4: count is 0
#5: count is 0
#6: count is 0

unary test for prefix:
#0: count is 1
#1: count is 2
#2: count is 3
#3: count is 0
#4: count is 1
#5: count is 2
#6: count is 3

for loop test:
#0: count is 1
#1: count is 2
#2: count is 3
#3: count is 0
#4: count is 1
#5: count is 2
#6: count is 3
*/

Loading data in Flex 101

December 16, 2008

First of all, all the Flash ways of loading data are available for Flex.

Then there are some Flex specific ways (depending on whether it’s Adobe Flash Player application or Adobe AIR application).

Using the HTMLControl Class in Adobe AIR to parse HTML as a data source

click on marker always show the last entry of data

June 14, 2008

This is a question from a co-worker of my last job. His is using gcliendGeocoder within a for-loop to dynamically get lat/lng and creating makers on Google maps. The problem is each time you click on a marker, the html text in the info window is always the one that should be for the last marker in the for-loop.

His code:

for (id = 0; id < addresses.length; id++)
{
var description = addresses[id];
var myBiz = biz[id];
geocoder.getLatLng(
description,
function(point)
{
var marker = new GMarker(point);
GEvent.addListener(marker, ‘click’,function(){marker.openInfoWindowHtml(description);});
map.addOverlay(marker);
}
);
}

The point here is “geocoder.getLatLng” is an “asynchronous” callback. That means when the “for-id-loop” reaches its end, the very first “getLatLng” may not get back yet. That’s why you always get the last description in the array. The fix is to add an additional counter inside the call back of “getLatLng”, and assign it as a dynamic property to the marker that gets returned from the callback, and then increment the counter by one at the end of the callback. When you handle the click, you always do a lookup on the marker using the id to find the right marker and the description.

var markers = [];
var closureID = 0;

for (id = 0; id < addresses.length; id++) {
var description = addresses[id];

geocoder.getLatLng(
description,
function(point) {
if(point)
{
var marker = new GMarker(point);
marker.id = closureID;
markers[closureID]=marker;
GEvent.addListener(marker, ‘click’,
function()
{
markers[marker.id].openInfoWindowHtml(addresses[marker.id]);
}
);
map.addOverlay(marker);
++closureID;
}
}
);

}

To see it in action, here is what happens “before“, and “after“: