473,387 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 15029


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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.