472,378 Members | 1,324 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,378 software developers and data experts.

using "this" in javascript/XHTML

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
Jul 20 '05 #1
6 14918


Mike Kamermans wrote:
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.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 20 '05 #2
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[i];
var pnode = node.parentNode;
pnode.insertBefore(imnode,node);
}

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

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
Jul 20 '05 #3
Err, perhaps also useful is to say what the intention was =)

I want to replace all [add] elements in my xml document with [img]
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
Jul 20 '05 #4


Mike Kamermans wrote:
imnode = xmldoc.createElement('img');


If you want to create an XHTML <img> element in an XML document then you
need to use
innode = xmldoc.createElementNS('http://www.w3.org/1999/xhtml', 'img')
as you need to make sure the element has the proper XHTML namespace.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 20 '05 #5
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
Jul 20 '05 #6


Mike Kamermans wrote:
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.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: leegold2 | last post by:
I don't know how to explain this action, basically a web page will show a brief format and there'll be a link the says "show more" and when clicked more text shows. eg. goto ...
7
by: Fendi Baba | last post by:
The function is called from opencalendar(targetfield). Thanks for any hints on what could be the problem. .............................................................. var...
0
by: Colleyville Alan | last post by:
My app is giving me this error. Run-time error 3211: The database engine could not lock table 'Sorted_Template' because it is already in use by another person or process. When I run the app...
6
by: Thomas H | last post by:
Hi everyone, I've got a question that's more of a "style" question... Do you guys reference "this" for every object that's inherited from System.Web.UI.Page? For example, when you use the...
6
by: Marty | last post by:
Hi, I have a class that I modified to be static. It is now a public sealed class and all function are static, no more constructor but a init() function to do the constructor job. This class...
60
by: Dave | last post by:
I'm never quite sure whether to use "this." or not when referring to fields or properties in the same class. It obviously works just fine without it but sometimes I wonder if using this....
2
by: Dustin | last post by:
I found a solution to my problem in the topic called "OOP (and the XMLHttpRequest)". You may find it useful to reference to see what I have done so far. I am making a small AJAX helper object...
14
by: Alexander Dong Back Kim | last post by:
Dear all, I used to use C++ programming language at all time but moved to C# and Java. Few days ago, I restarted studying about C++ with a very beginner's mind. I wrote a simple class and gcc...
3
dlite922
by: dlite922 | last post by:
Hey guys, My brains asleep and I don't know what's wrong with my session class. I'm over riding session with sesstion_set_save_handler() in a class; When in my member functions (open,...
3
by: George | last post by:
Being nebie in Javascript i am trying to code my own helper object that does pagination. So here is a snippet of my code. MyData.prototype = { ....blablabla... PageUp: function() { this.iFrom...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.