using "this" in javascript/XHTML

M

Mike Kamermans

I'm trying to determine where inside an XHTML's DOM a javascript function
is triggered, but when I use the construction of calling a function with
"this" as parameter and then checking the event target inside the
javascript function, I get very very little love from javascript in an
XHTML setting.... I have put up the file that demonstrates my problem at:

http://www.nihongoresources.com/downloads/test/test.xml

(the jscript bit is http://www.nihongoresources.com/downloads/test/test.js)

If anyone knows what I'm doing wrong here, I'd appreciate any help =/

- Mike Kamermans
www.nihongoresources.com
 
M

Martin Honnen

Mike said:
I'm trying to determine where inside an XHTML's DOM a javascript function
is triggered, but when I use the construction of calling a function with
"this" as parameter and then checking the event target inside the
javascript function, I get very very little love from javascript in an
XHTML setting.... I have put up the file that demonstrates my problem at:

http://www.nihongoresources.com/downloads/test/test.xml

(the jscript bit is http://www.nihongoresources.com/downloads/test/test.js)

If anyone knows what I'm doing wrong here, I'd appreciate any help =/

First of all
window.event
is something IE supports but not something the W3C DOM wants so if you
want to script Mozilla make sure you pass the event object as a
parameter e.g.
<a onclick="change(event);"
with
function change (evt) {
// access for instance evt.target here
alert(evt.target);
}
As for the this object, you have
<a onclick="change(this)"
and traditionally the toString() value of an <a> element is its href
attribute, as you don't have a href attribute the alert shows nothing,
try instead
<a onclick="change(this);">
with
function change (obj) {
alert(obj.tagName)
and you will see that the object passed in is indeed an <a> element.
 
M

Mike Kamermans

Using event certain made things work.. =D

I don't suppose you'd know why the following code doesn't get rendered? I
call a replace function using an onload event in the body, which passes
the entire xml document as event (I had expected to get the body node,
but this also works I suppose). So I have in the xml:

<body onload=replace(event)>

And then in the js file I have a replace function that does this:

function replace(e) {
xmldoc = e.target;
replacenodes = xmldoc.getElementsByTagName('replace');

//forward traversal insert
for(var i=0;i<replacenodes.length;i++){
imnode = xmldoc.createElement('img');
imnode.setAttribute('src','add.gif');
var node = replacenodes;
var pnode = node.parentNode;
pnode.insertBefore(imnode,node);
}

//reverse traversal delete
for(var i=replacenodes.length-1;i>=0;i--){
replacenodes.parentNode.removeChild(replacenodes);
}
}

Now, this works, in the sense that <img src="add.gif"> elements get added
to the source (using a css file to draw the img elements as boxes for
instance shows boxes where they should be), but unlike for instance
adding an element called "p", and it consequently being rendered as a
paragraph upon insertion, the "img" element does not get rendered as an
image...

Any clues?

Much oblidged,

- Mike Kamermans
www.nihongoresources.com
 
M

Mike Kamermans

Err, perhaps also useful is to say what the intention was =)

I want to replace all [add] elements in my xml document with
elements that have as attribute set [src="add.gif",
onclick="recommendAddition(event)", alt="recommend an addition",
width="9px", height="9px, border="0"].

I figured I'd "fake" a replacementan by making [img] nodes, inserting
them before each [add] node, and then deleting all the [add] nodes from
the document tree once that was done.

(the justification for this is that it would save a lot of bandwidth for
the server, because it has a heck of a lot less data to send, and users
would be able to see the requested document faster)

- Mike Kamermans
www.nihongoresources.com
 
M

Mike Kamermans

Hmm.. Thanks, that works. Though it's odd one needs to define the namespace
when the element is inserted in a DOM that uses the xhtml NS as default NS,
set up through the <html xmlns="..."> top level tag.

Mike
 
M

Martin Honnen

Mike said:
Though it's odd one needs to define the namespace
when the element is inserted in a DOM that uses the xhtml NS as default NS,
set up through the <html xmlns="..."> top level tag.

Well check the DOM documentation and you will find that createElement is
not namespace aware, even if it appears odd to you. And if you insert an
element the namespaces in scope of the parent do not matter.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,998
Messages
2,570,242
Members
46,834
Latest member
vina0631

Latest Threads

Top