The browser is saying there is an error in my javascript at this function, in my xhtml. If I remove this function the error disappears. For me this piece of code looks like perfect in syntax:
function toAscii(text) {
for (var i=0; i<text.length; i++) {
var charCode = text.charCodeAt(i);
var ascii = charCode.toString(2);
console.log(ascii);
}
}
Chrome error:
This page contains the following errors:
error on line 19 at column 31: error parsing attribute name
Below is a rendering of the page up to the first error.
Firefox error:
Erro no processamento de XML: formatação incorreta
Posição: file:///C:/Users/Carlos/Desktop/ascii%20converter.xhtml
Número da linha 19, coluna 33:
for (var i=0; i<text.length; i++) {
---------------------------------------------------^
This is the entire XHTML:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
".dtd">
<html xmlns="">
<head>
<title>ASCII Converter</title>
<meta charset="UTF-8"/>
<script type="text/javascript">
function converter() {
var text = document.getElementById('texto').value;
toAscii(text);
}
function toAscii(text) {
for (var i=0; i<text.length; i++) {
var charCode = text.charCodeAt(i);
var ascii = charCode.toString(2);
console.log(ascii);
}
}
</script>
</head>
<body>
Texto (Entrada):<br/>
<input id="texto" type="text"/><br/>
ASCII (Saída):<br/>
<input id="ascii" type="text"/><br/>
<input type="button" id="botao" value="Converter" onclick="converter()"/><br/>
</body>
</html>
The browser is saying there is an error in my javascript at this function, in my xhtml. If I remove this function the error disappears. For me this piece of code looks like perfect in syntax:
function toAscii(text) {
for (var i=0; i<text.length; i++) {
var charCode = text.charCodeAt(i);
var ascii = charCode.toString(2);
console.log(ascii);
}
}
Chrome error:
This page contains the following errors:
error on line 19 at column 31: error parsing attribute name
Below is a rendering of the page up to the first error.
Firefox error:
Erro no processamento de XML: formatação incorreta
Posição: file:///C:/Users/Carlos/Desktop/ascii%20converter.xhtml
Número da linha 19, coluna 33:
for (var i=0; i<text.length; i++) {
---------------------------------------------------^
This is the entire XHTML:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3/1999/xhtml">
<head>
<title>ASCII Converter</title>
<meta charset="UTF-8"/>
<script type="text/javascript">
function converter() {
var text = document.getElementById('texto').value;
toAscii(text);
}
function toAscii(text) {
for (var i=0; i<text.length; i++) {
var charCode = text.charCodeAt(i);
var ascii = charCode.toString(2);
console.log(ascii);
}
}
</script>
</head>
<body>
Texto (Entrada):<br/>
<input id="texto" type="text"/><br/>
ASCII (Saída):<br/>
<input id="ascii" type="text"/><br/>
<input type="button" id="botao" value="Converter" onclick="converter()"/><br/>
</body>
</html>
Share
Improve this question
edited Oct 20, 2013 at 2:03
ceklock
asked Oct 20, 2013 at 1:46
ceklockceklock
6,38211 gold badges57 silver badges79 bronze badges
4
- 4 Isn't the error on your XHTML? – bfavaretto Commented Oct 20, 2013 at 1:47
- Maybe. But if I remove this function the error disappears. – ceklock Commented Oct 20, 2013 at 1:47
- 1 Did you try wrapping your js in a cdata node? I think XML requires that, but I'm not sure. Do you really need XHTML? – bfavaretto Commented Oct 20, 2013 at 1:56
- I don't really need xhtml. Maybe I will switch back to simple html. – ceklock Commented Oct 20, 2013 at 1:57
2 Answers
Reset to default 4Since you're using xhtml and you have a <
in your code you'll have to enclose your code in a cdata block
<script type="text/javascript"><![CDATA[
function converter() {
var text = document.getElementById('texto').value;
toAscii(text);
}
function toAscii(text) {
for (var i=0; i<text.length; i++) {
var charCode = text.charCodeAt(i);
var ascii = charCode.toString(2);
console.log(ascii);
}
}
]]></script>
XHTML, strictly speaking, doesn't allow javascript to be stuck in the header the way one might to with HTML. As someone mentioned, it should be wrapped in a CDATA section. As a side note, the code as you've posted runs on my browser, but it's probably being lenient. Check out this reference for more information on xhtml and embedded css/javascript:
https://developer.mozilla/en-US/docs/Properly_Using_CSS_and_JavaScript_in_XHTML_Documents