473,387 Members | 1,502 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.

Moving text with javascript

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?
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /
>
<script type="text/javascript">
function TextMove(val) {
try {
var divObj = document.getElementById("mouseCoord");
divObj.innerHTML = "Moving";
divObj.style.left=parseInt(val);
}
catch (ex) { alert (ex); }
}
</script>
</head>
<body onload="TextMove(0);">
<div>
<div id="mouseCoord" onmouseover="TextMove(10);"
onmouseout="TextMove(0);" style="position:relative;">&nbsp;</div>
</div>
</body>
</html>

May 3 '07 #1
1 11011
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

May 3 '07 #2

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

Similar topics

1
by: Andi B | last post by:
Hi everyone. I have a quick question - if I have a form which refers to a javascript when its submitted using: <form name = "fred" onsubmit = "test()"> and this function creates a popup...
4
by: Jake j | last post by:
To those of you who sent me working examples of js include routines, much thanks. I see from them examples that what I'm trying to get to requires a higher level of js knowledge than I've got. ...
10
by: Mark McLellan | last post by:
Dear all Following the oft-repeated advice here and ciwas I have made my site nearly 4.01 strict (working on it). There are some items on which I would appreciate your advice: 1. Hidden...
1
by: OwlHoot | last post by:
I am using Thomas Fuchs's amazing drag-and-drop JavaScript library available at: http://wiki.script.aculo.us/scriptaculous/show/DragAndDrop to allow the user to select a subset of items listed...
1
by: JohnIdol | last post by:
Hi all, a quick one here. I am moving elements on the page clientside and I am going through strange issues. This one works: <html> <head> <title>Javascript Text</title>
2
by: jonny | last post by:
I am using Visual Web Developer with ASP and VB.NET to build website. I would like to have Text moving across my screen that says "Example text moving across screen for viewers to read.". I...
1
by: madflytom | last post by:
Hello, I'm trying to move the options of one select list to another select list. The "source" select list is divided into optgroups, and the "target" select list is not. I want to somehow keep...
10
by: cjparis | last post by:
Hello everyone. If anyone can give me a hand I would be gratefull Am doing a site which requires a moving element and have used DHTML to do it. Have a simple Browser detect script to sort IE...
3
by: mrrolf | last post by:
Hi all! I have some pictures on a page that I'm moving around with javascript. I'm just setting the style.top member. I don't want the image to overflow the parent container if I move it to a...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...

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.