Saturday, February 24, 2007

How to test javascript performance time

First, create area where results will be output. You can adjust any style property to fit your needs. Minimum what you need to change is "parentNodeId":

// Test env
function testEnv() {
 var parentNode = document.getElementById("parentNodeId");
 if (parentNode) {
  var testNode = document.createElement("div");
  testNode.id = "testEnv";
  testNode.style.height = "7em";
  testNode.style.backgroundColor = "white";
  testNode.style.overflow = "auto";
  testNode.style.border = "1px solid #778899";
  testNode.style.textAlign = "left";
  testNode.style.overflowX = "hidden";
  testNode.style.whiteSpace = "nowrap";
  parentNode.appendChild(testNode);
 }
}

Then, in the function or any piece of the code you are testing, write:
in the beginning:

var beginTime = new Date().valueOf();

and finally, at the end:

if (document.getElementById("testEnv"))
  document.getElementById("testEnv").innerHTML =
    "execution time: " +
    (new Date().valueOf()-beginTime)/1000 +
    " sec<br/>" +
    document.getElementById("testEnv").innerHTML;

Thursday, February 15, 2007

"The webpage you are viewing is trying to close this window"

In Internet Explorer 6 there is the way to close window by javascript without warning: "The webpage you are viewing is trying to close this window":


window.opener = top; window.close();

But not in IE 7 unless window was opened by your javascript from other window.

Adding event listener on the page

Suppose you have an input field and you need FilerTag function be performed each time user types a character. Below are two approaches for this.

Native browser approach:


if (a.addEventListener) {a.addEventListener("keyup", FilterTags, false)} // firefox
else if (a.attachEvent) {a.attachEvent("onpropertychange", FilterTags)} // ie

YUI approach:

YAHOO.util.Event.addListener(a, "keyup", FilterTags);