Monthly Archives: November 2006

Attempt to use JSON

Ok, so JSON is conceptually simple. See json.org for more info. So I include json.js in a page, then attempt to reference the parseJSON method of a string object and get an error. Why? Is it because I reference the method in another external script file, instead of using inline script in the page itself? How exactly does Javascript run the code to extend the prototype of an intrinsic object? Is it prior to the page loading? Help!   BTW – I can get this working no problem in a simple page where the script is inline.

Found a solution to the click problem.

I confirmed that click is not a W3C method for anchor objects. However, thanks to the prototype property of objects you can extend a host object by adding methods to it. The code below does this for anchor elements, although you could do it for any element. Credit to Martin Honnen for his article at http://www.faqts.com/knowledge_base/view.phtml/aid/9368 for the solution. Here’s the code, which just needs to be in the top of your page so that it runs prior to any scripts at document load time:


try {
// create a element so that HTMLAnchorElement is accessible
document.createElement('a');
HTMLElement.prototype.click = function () {
if (typeof this.onclick == 'function') {
if (this.onclick({type: 'click'}) && this.href)
window.open(this.href, this.target ? this.target : '_self');
}
else if (this.href)
window.open(this.href, this.target ? this.target : '_self');
};
}
catch (e) {
alert('click method for HTMLAnchorElement couldn\'t be added')
}

My First Post

Javascript Stuff

Tip: Local versus global variables- use the var keyword to declare a variable as local.  

Tip: don’t write this:     var x;   

because x will be undefined. Write this instead: var x = 0; // or some other initial value.

Question: the click method for anchor tags does not appear to work in FireFox. Can anyone confirm this? Here’s a piece of code that fails:
var anchortag = document.getElementsByTagName("a")[0];
anchortag.click();