ba***************@gmail.com wrote:
I'm definitely not new to JS,
I would say you are new to it enough.
but for the life of me, I can't figure
this one out. Here's basically what I'm doing:
function foo() {
alert(this);
}
span = document.createElement('span');
Identifiers should be declared:
var span = ...;
At least methods should be feature-tested:
function isMethod(a)
{
return a && /^\s*(function|object)\s*$/.test(typeof a);
}
if (isMethod(document.createElement))
{
... document.createElement(...) ...
}
a = document.createElement('a');
See above.
// Code to add text, etc to the tag here...
a.onclick = foo;
References should be tested before they are used:
<URL:http://pointedears.de/scripts/test/whatami#inference>
span.appendChild(a);
See above.
// ... stuff ...
divObject.appendChild(span);
See above.
Instead of seeing like [object HTMLElement] or whatever, I simply see
the URL of the page in the alert window.
The reason is that in many DOMs `HTMLAnchorElement' objects (and their Link
equivalents) inherit a toString() method from their prototype that returns
the value of their `href' property (with relative URIs resolved). Since
you have not set that property, it has its default value which is "" which
in turn is a relative URI for the resource where the element is located.
Which is, quite correctly, "the URL of the page", as window.alert()
converts its argument to string.
I've also tried adding (e) as
a parameter to foo to capture the event, but with no luck. And I tried
using an anonymous function:
a.onclick = function() { alert(this); }
As I expected, that does not make any difference. `this' refers to the
calling object (which is the event target with this syntax) and its `href'
property is still the empty string.
There are several possibilities, among them:
- You can use FireBug's Event Inspector
- You can access this.nodeName
- In the Gecko DOM, you can overwrite HTMLAnchorElement.prototype.toString()
(backing it up first) to show any data you find convenient, for example:
HTMLAnchorElement.prototype.toString = function()
{
return '<a href="' + this.href + '">';
}
See also <URL:http://pointedears.de/ObjectInspector?root=HTMLAnchorElement>.
PointedEars
--
Germany is the birth place of psychiatry. Psychs feel threatened by
Scientology as they are crazy. Many psychs become psychs to heal their own
mental problems and to control others. -- "The only real Barbara Schwarz",
dsw.scientology, <16**************************@posting.google.com >