Access parent window from modalDialog

August 15, 2008 · Posted in general web, javascript · Comment 

When you call showModalDialog() you need to pass “self”, without the quotes, as the second argument.

You can then access the parent(opener) with:
var opener = window.dialogArguments;

You can then access any function declared in parent window with:
opener.myFunction();

Rules for Speeding Up Your Web Site

September 4, 2007 · Posted in css, general web, javascript · Comment 

Yahoo Developer Network posted a nice set of rules to speed up your website. The excerpt is as follows:

  1. Make Fewer HTTP Requests
  2. Use a Content Delivery Network
  3. Add an Expires Header
  4. Gzip Components
  5. Put Stylesheets at the Top
  6. Put Scripts at the Bottom
  7. Avoid CSS Expressions
  8. Make JavaScript and CSS External
  9. Reduce DNS Lookups
  10. Minify JavaScript
  11. Avoid Redirects
  12. Remove Duplicate Scripts
  13. Configure ETags

You can read the full article here.

Validate e-mail address with regular expression

August 2, 2007 · Posted in java, javascript · Comment 

/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/

All regular expressions start and end with forward slashes to differentiate them from ordinary string expressions. Most regular expressions start matches at the first character ^ and end at the last $.

Now we try to match the mailbox name which can include periods and dashes \w+ states one or more alphanumeric must be at the start of the name. ([\.-]?\w+)* allows periods or dashes to be included in the mailbox name with the trailing \w+ ensuring that those characters can not finish the name. The @ is the mandatory separator.

The domain name can have several .xx or .xyz suffixes such as .com.uk. Once again \w+ ensures that domain starts with an alphanumeric and ([\.-]?\w+)* allows for the dashes and periods. Finally (\.\w{2,3})+ ensures that there is at least one suffix of between 2 and 3 characters preceded by a period.

Note: This is not a completely foolproof validation as it does not account for new domain names of 4 or more characters. Also not all two and three letter combinations are legitimate domains!

Trim a string with all types of spaces

August 2, 2007 · Posted in javascript · Comment 

var a = “151 “;
a = a.replace(/^\s+|\s+$/, “”);

//a is now “151″ .

Note:
\s : Matches a single white space character, including space, tab, form feed, line feed.
Equivalent to [ \f\n\r\t\v\u00A0\u2028\u2029].

\S: Matches a single character other than white space.
Equivalent to [^ \f\n\r\t\v\u00A0\u2028\u2029].