On May 3, 6:42 pm, "mmal...@gmail.com" <mmal...@gmail.comwrote:
I have the following code to move some text with javascript that works
both on ie and ff. I can't make it XHTML compatble. If i remove the
doctype info it will work. I'm sorry to bother with such simple
question but is there someone to see where am i wrong?
Use HTML.
>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>title</title>
Use 2 spaces for indenting posted code, it doesn't wrap quite so
readily.
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /
<script type="text/javascript">
function TextMove(val) {
try {
var divObj = document.getElementById("mouseCoord");
try...catch should be reserved as an absolute last resort - feature
detection is much preferred:
var divObj;
if (!document.getElementById ||
!(divObj = document.getElementById("mouseCoord"))) {
return;
}
divObj.innerHTML = "Moving";
If you really are using XHTML (i.e. you are serving it as application/
xml), then you probably shouldn't be using innerHTML. There's no
standard for innerHTML, so whether it *should* work with XHTML or not
is moot - but it comes from a company whose browser doesn't understand
XML.
I would try straight DOM methods:
while (divObj.firstChild) {
divObj.removeChild(divObj.firstChild);
}
divObj.appendChild(document.createTextNode('Moving '));
Though the innerHTML alternative worked fine for me in Safari and
Firefox.
divObj.style.left=parseInt(val);
The style object's left property expects to get a string value, and
any value other than zero must have a unit (I'll assume you want px):
divObj.style.left = val + 'px';
Here's the function in one piece:
function TextMove(val) {
var divObj;
// Feature test
if ( document.getElementById &&
(divObj = document.getElementById("mouseCoord"))) {
// Remove contents
while (divObj.firstChild) {
divObj.removeChild(divObj.firstChild);
}
// Add required text
divObj.appendChild(document.createTextNode('Moving '));
// Jiggle the element
divObj.style.left = val + 'px';
}
}
--
Rob