PDA

View Full Version : cant find out whats wrong in IE



Dennis_Gull
07-04-2007, 03:54 PM
hello, I just read about half the book called "Ajax for dummies" and Im now able to make my own ajax application (I can recommend that book) but I still got some problems in IE and well javascript is a kinda complex language so I have no idea why I get this error (but only in IE) It just says:
Line: 24.
char: 5.
Error: unknown runtime error.
code: 0.
<my url>

my line 24 in the js file looks like this:
for (var i = 0; i < comment.length; i++) {

The script works perfect in FF and I use firebug so I would see if there was any problems in the code.
I got another script to check if username is available and that works perfect in all browsers.

jscheuer1
07-04-2007, 04:21 PM
To work in IE 7, Ajax sometimes must be live. In IE in general, the error line number may often refer to the previous line in the code, or not even to any readily discernible line in the code.

Without a link to a demo page of this problem, it would be hard to be more specific.

Dennis_Gull
07-04-2007, 11:23 PM
I can post the js file here (but remember that I'm a newbie with both java and ajax) :)



function comment(id, rows) {
var obj = document.getElementById("comment");
var pages = document.getElementById("pages");
pages.innerHTML = '';
XMLHttpRequestObject = false;
if (window.XMLHttpRequest) {
XMLHttpRequestObject = new XMLHttpRequest();
} else if (window.ActiveXObject) {
XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
}

if (XMLHttpRequestObject) {
var cid = "xml_com.php?id=" + id + "&v=" + rows;
XMLHttpRequestObject.open("GET", cid, true);
XMLHttpRequestObject.onreadystatechange = function() {
if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
var xdoc = XMLHttpRequestObject.responseXML;
var comment = xdoc.getElementsByTagName("comment");
var title = xdoc.getElementsByTagName("title");
var username = xdoc.getElementsByTagName("user");
var total = xdoc.getElementsByTagName("total");
var t = total[0].firstChild.data;
obj.innerHTML = "<table>";
for (var i = 0; i < comment.length; i++) {
obj.innerHTML += "<tr><td width=\"400\" class=\"bold\">" + title[i].firstChild.data + "</td></tr><tr><td>" + comment[i].firstChild.data + "</td></tr><tr><td align=\"left\" class=\"smalltext\" style=\"padding-left:300px;\">posted by: " + username[i].firstChild.data;
}
obj.innerHTML += "</table>";
if (t > 10 && rows == 0) {
pages.innerHTML += " <a class=\"link\" onclick=\"comment('" + id + "','1')\">show all</a>";
} else if (t > 10 && rows == 1) {
pages.innerHTML += " <a class=\"link\" onclick=\"comment('" + id + "','0')\">collapse</a>";
}
}
}
XMLHttpRequestObject.send(null);
}
}

function post_comment(id, user) {
var obj = document.getElementById("post_comment");
obj.innerHTML = "<form><tr><td>Title:</td><td><input type=\"text\" id=\"title\" name=\"com_title\" size=\"48\"/></td></tr><tr><td valign=\"top\">Comment:</td><td><textarea name=\"com_comment\" id=\"message\" rows=\"4\" cols=\"36\"></textarea></td></tr><tr><td align=\"right\" colspan=\"2\"><input type=\"submit\" value=\"cancel\" onclick=\"post_cancel()\"><input type=\"submit\" value=\"post\" onclick=\"post_submit('" + id + "', '" + user + "')\"></td></tr></form>";
}

function post_cancel(){
document.getElementById("post_comment").innerHTML = '';
}

function post_submit(id, user) {
var message = document.getElementById('message').value;
var title = document.getElementById('title').value;
var passData = 'title='+ title +'&name='+ user +'&comment='+ message + '&id=' + id;
XMLHttpRequestObject = false;
if (window.XMLHttpRequest) {
XMLHttpRequestObject = new XMLHttpRequest();
} else if (window.ActiveXObject) {
XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
}
if (XMLHttpRequestObject) {
XMLHttpRequestObject.open("POST", "postc.php", true);
XMLHttpRequestObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
XMLHttpRequestObject.onreadystatechange = function () {
if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
document.getElementById("post_comment").innerHTML = '';
comment(id, '0');
}

}
XMLHttpRequestObject.send(passData);
}
}

thetestingsite
07-04-2007, 11:46 PM
As said before, we need a link to the problem page to really be able to help.

jscheuer1
07-05-2007, 03:28 AM
This order of XML HTTP object testing:



XMLHttpRequestObject = false;
if (window.XMLHttpRequest) {
XMLHttpRequestObject = new XMLHttpRequest();
} else if (window.ActiveXObject) {
XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");

Requires a live installation to work in IE 7. IE 7 will follow the XMLHttpRequestObject = new XMLHttpRequest(); path because it can. But - it will not allow it to execute locally due to security restrictions.

There could be other problems.

Added Later:

I also noticed this sort of thing in your code:



obj.innerHTML = "<table>";

That is actually the error you are getting, I believe. It is invalid HTML, no closing tag. The fact that you follow it later with:



obj.innerHTML += "<tr><td width=\"400\" class=\"bold\">" + title[i].firstChild.data + "</td></tr><tr><td>" + comment[i].firstChild.data + "</td></tr><tr><td align=\"left\" class=\"smalltext\" style=\"padding-left:300px;\">posted by: " + username[i].firstChild.data;

and:



obj.innerHTML += "</table>";

doesn't matter. The proper way to do that is to create a variable or an object that holds the value for the innerHTML value until it is complete:


obj.HTML = "<table>";
for (var i = 0; i < comment.length; i++) {
obj.HTML += "<tr><td width=\"400\" class=\"bold\">" + title[i].firstChild.data + "</td></tr><tr><td>" + comment[i].firstChild.data + "</td></tr><tr><td align=\"left\" class=\"smalltext\" style=\"padding-left:300px;\">posted by: " + username[i].firstChild.data;
}
obj.HTML += "</table>";
obj.innerHTML=obj.HTML;

You have made this same sort of 'error' with pages.innerHTML and perhaps elsewhere.

Dennis_Gull
07-05-2007, 07:34 PM
okay thanks, I will correct it and see if it works. And I will also try to see if it works on a live server (got It on a local now)

Dennis_Gull
07-12-2007, 11:54 PM
I know there was like a week ago I asked this but I haven't been coding since then. But started again today and found out why I got this error, apparently I cant create a table inside the javascript (found a comment line on someone else work and it said that IE didnt support tables to be created, or something like that) So I removed the tables and it worked but now everything is a big mess with the code above (IE skip all the td tags and such). could anyone give me some tip on how to make new tables and td tags in javascript (innerHTML)


Edit:

looked at some other threads and found that I can create a td like this:


document.createElement("TD")

How would I use this to make this:



var obj_inner = "<table><tbody>";
for (var i = 0; i < comment.length; i++) {
obj_inner += "<tr><td width=\"400\" class=\"chead\">" + title[i].firstChild.data + "</td></tr><tr><td>" + comment[i].firstChild.data + "</td></tr><tr><td align=\"left\" class=\"smalltext\" style=\"padding-left:300px;\">posted by: " + username[i].firstChild.data;
}
obj_inner += "</tbody></table>";
obj.innerHTML = obj_inner;


(sorry for a stupid question, but im kinda new to this, im going to read the rest of the book soon :))

jscheuer1
07-13-2007, 03:51 AM
That's nonsense. You still have invalid HTML code. No closing td or tr tags:



. . . </td></tr><tr><td align=\"left\" class=\"smalltext\" style=\"padding-left:300px;\">posted by: " + username[i].firstChild.data;?

This test worked fine in IE (notice the simplified approach to quoting):


<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd'>
<html>
<head>
<title></title>
<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
<script type='text/javascript'>
comment=['blah', 'blah blah', 'bleep', 'burp'];
function make_table(obj){
var obj_inner = '<table border="1"><tbody>';
for (var i = 0; i < comment.length; i++)
obj_inner += '<tr><td width="400" class="chead">' + comment[i] + '<\/td><\/tr><tr><td>' + comment[i] + ' huh?<\/td><\/tr><tr><td align="left" class="smalltext" style="padding-left:300px;">posted by: ' + comment[i]+ ' lah lah<\/td><\/tr>';
obj_inner += '<\/tbody><\/table>';
obj.innerHTML = obj_inner;
}
</script>
</head>
<body>
<input type="button" value="Do It" onclick="make_table(document.getElementById('bob'));">
<div id="bob"></div>
</body>
</html>

You can do it the other way that you mention as well, but if any browser is likely to have a problem with that (I'm not saying that any will) it would be IE. And, to create such an involved markup using the DOM would be complex, a bit beyond the scope of this forum.

If you want to learn how, here is a good reference:

http://developer.mozilla.org/en/docs/Traversing_an_HTML_table_with_JavaScript_and_DOM_Interfaces

Dennis_Gull
07-13-2007, 07:25 PM
Thanks for the help jscheuer1, with your code i figured out that the problem was the table outside the div that was the problem and not the actually code (but as you pointed out i missed the closing tag)

PS:
If I use ' and not " will i have to write for example: <\/td> instead of </td>?
like this:
variable = '<td>blah blah<\/td>';
or is this also okay?:
variable = '<td>blah blah</td>';

jscheuer1
07-14-2007, 12:35 AM
Using (escaping) \/ in place of / when closing a tag in a javascript string is just a good habit. If the script is on the page, using just / will usually confuse the validator, should you, or anyone later want to validate a page with this code on it.

It really is only required (from a practical standpoint and/or in an external script) if the closing tag in the string is a closing </script> tag. At that point, if the / isn't escaped, the script parser will think it is the end of the script. However, the technical specification allows the parser to stop as soon as it sees:

</

So, it is still a very good habit to escape all closing tag's /'s in strings in you code.

Dennis_Gull
07-14-2007, 05:17 AM
thanks for the reply, everything is sorted out and works good now. :)