Re: Question involving document.createElement and this
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.javascript archive

Re: Question involving document.createElement and this

From: Lasse Reichstein Nielsen <lrn@hotpop.com>
Date: Mon May 01 2006 - 08:03:48 CEST

bayfaulkscatering@gmail.com writes:

> function foo() {
> alert(this);
> }
...
> a = document.createElement('a');
> // Code to add text, etc to the tag here...
> a.onclick = foo;
...
> Instead of seeing like [object HTMLElement] or whatever, I simply see
> the URL of the page in the alert window.

The URL of *which* page? The current or the one being linked to by the
link?

I believe you are seeing a legacy feature. The original browsers
introducing a method to refer to "a" elements (the document.links
collection) predates the W3C standards. They could do whatever
they wanted, and choose to have the toString method of "a" elements
return the href property. Modern browser have kept this feature.

> And I tried using an anonymous function:
>
> a.onclick = function() { alert(this); }
...
> but it seems like the 'this' reference shouldn't even matter until the
> function is called, at which time it is set to the a object.

Correct.

/L

-- 
Lasse Reichstein Nielsen  -  lrn@hotpop.com
 DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
  'Faith without judgement merely degrades the spirit divine.'
Received on Mon May 1 05:28:41 2006