Newbie problem with IE

K

Kiwi_In_VT

Hi,

I'm a software guy, but brand new to JavaScript. I have my first
script cut, pasted and tweaked together and is WORKING GREAT on
Firefox. I thought before I used it on the real pages on my site I
should try it with IE.
I just get an 'error on page' message, and I don't know where to start
looking for the problem...
Any help would be appreciated :)

The pages on my site have a series of ads down one side. The goal of
the script is to download the html for the ads (currently 7 of them)
and then every minute reorder them randomly, concatenate them in the
new order, and stuff them in the table.

Here's the code (please excuse the old-school indentation scheme, it's
what I grew up with, I like it, and I'm too old to change):

window.onload = initAll;

var xhr = false;
var smallAds = [];

function initAll() {
getNewFile("ajaxSmallAds.php"); // gets it once, then keeps re-
ordering every minute
}

function getNewFile(fileName) {
makeRequest(fileName);
return false;
}

function randOrd(){
return (Math.round(Math.random())-0.5);
}

function showSmallAds() {
var s = "";
for (i=0; i<smallAds.length; i++) {
s += smallAds;
}
document.getElementById("buttonAdTable").innerHTML = s;
smallAds.sort(randOrd); // randomize for next call
setTimeout("showSmallAds()", 60 * 1000); // display the ads
randomly every 1 minute
}

function makeRequest(url) {
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
}
else {
if (window.ActiveXObject) {
try {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) { }
}
}
if (xhr) {
xhr.onreadystatechange = showContents;
xhr.open("GET", url, true);
xhr.send(null);
}
else {
document.getElementById("raceX").innerHTML = "Sorry, but I
couldn't create a request";
}
}

function showContents() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
var outMsg = (xhr.responseXML &&
xhr.responseXML.contentType=="text/xml") ?
xhr.responseXML.getElementsByTagName("choices")[0].textContent :
xhr.responseText;
}
// else {
// var outMsg = "There was a problem with the request " +
xhr.status;
// }
var data = [];
data = outMsg.split("\n");
if (data[0] == "smallAds") {
for (i=1; i<data.length; i++) {
if (data != "") {
smallAds[i-1] = data;
}
}
// now call the function that will randomly place the ads from
now on... :)
showSmallAds();
}
}
}
 
T

Thomas 'PointedEars' Lahn

Kiwi_In_VT said:
I'm a software guy, but brand new to JavaScript. I have my first ^^^^^^^^^^^^^^^^^^^^^^^
script cut, pasted and tweaked together and is WORKING GREAT on
Firefox.

You could not even tell.
I thought before I used it on the real pages on my site I
should try it with IE.
I just get an 'error on page' message, and I don't know where to start
looking for the problem...
Any help would be appreciated :)
http://www.jibbering.com/faq/faq_notes/clj_posts.html#ps1DontWork
http://getfirebug.com/

The pages on my site have a series of ads down one side. The goal of
the script is to download the html for the ads (currently 7 of them)
and then every minute reorder them randomly, concatenate them in the
new order, and stuff them in the table.

Here's the code (please excuse the old-school indentation scheme, it's
what I grew up with, I like it, and I'm too old to change): [...]

Nice try.


PointedEars
 
K

Kiwi_In_VT

Thanks 'PointedEars', I installed firebug, hasn't told me anything but
that's probably because I haven't learnt it yet.

Here's a link to the test page:

http://www.live-timing.com/race2.php

It's not fancy (I don't have the skills yet) but I'm surprised
something so simple works in Firefox but not IE.

Is there a way to know what causes the error in IE ?

Thanks in advance.
 
M

My Pet Programmer

Kiwi_In_VT said:
Thanks 'PointedEars', I installed firebug, hasn't told me anything but
that's probably because I haven't learnt it yet.

Here's a link to the test page:

http://www.live-timing.com/race2.php

It's not fancy (I don't have the skills yet) but I'm surprised
something so simple works in Firefox but not IE.

Is there a way to know what causes the error in IE ?

Thanks in advance.
I think your code is trying to access the table before it's loaded. In
similar situations, I have put the javascript below the body tag,
actually am just in that habit now, because it avoided so many problems
for me.

~A!
 
T

Thomas 'PointedEars' Lahn

My said:
[...] In similar situations, I have put the javascript below the body
tag, actually am just in that habit now, because it avoided so many
problems for me.

Your habit lets you produce invalid code.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Kiwi_In_VT said:
Thanks 'PointedEars', I installed firebug, hasn't told me anything but
that's probably because I haven't learnt it yet.

There is Firebug Lite for IE which might help. There is also the Microsoft
Script Debugger which tells me the problem is in this line in showSmallAds():

document.getElementById("buttonAdTable").innerHTML = s;
Is there a way to know what causes the error in IE ?

There is.
Thanks in advance.

You're welcome. Please quote the minimum of what you are replying to next time.


PointedEars
 
M

My Pet Programmer

Thomas 'PointedEars' Lahn said:
There is Firebug Lite for IE which might help. There is also the Microsoft
Script Debugger which tells me the problem is in this line in showSmallAds():

document.getElementById("buttonAdTable").innerHTML = s;

That's what told me he might be accessing the elements before they are
ready. What do you think?

~A!
 
T

Thomas 'PointedEars' Lahn

My said:
Thomas 'PointedEars' Lahn said:

That's what told me he might be accessing the elements before they are
ready. What do you think?

"Immediate" evaluation in the MSSD shows that the object reference is valid
at this point and

document.getElementById("buttonAdTable").innerHTML

yields

"<TBODY></TBODY>"

Since

s

yields

"<tr><td><a href=\"http://www.racewax.com\" target=\"_blank\"><img
src=\"images/racewax_live_timing_ad.jpg\" width=\"120\" height=\"90\"
border=\"0\"></a></td></tr><tr><td><a href=\"http://www.haltiusa.com\"
target=\"_blank\"><img src=\"images/HALTI.gif\" width=\"120\" height=\"90\"
border=\"0\"></a></td></tr><tr><td><a href=\"http://www.gocva.com\"
target=\"_blank\"><img src=\"images/CVA_button.gif\" width=\"120\"
height=\"90\" border=\"0\"></a></td></tr><tr><td><table border=\"0\"
cellpadding=\"0\" cellspacing=\"0\"><tr><td bgcolor=\"#ffffff\"><a
href=\"http://www.flexterm.org\" target=\"_blank\" alt=\"Green Mountain
Velley School\"><img src=\"images/GMVS-FlexTerm.gif\" width=\"120\"
height=\"90\" border=\"0\"></a></td></tr></table></td></tr><tr><td><a
href=\"http://www.uscsa.com\" target=\"_blank\"><img
src=\"images/USCSA120x90.jpg\" width=\"120\" height=\"90\"
border=\"0\"></a></td></tr><tr><td><a
href=\"http://www.edgewiseskitunes.com\" target=\"_blank\"><img
src=\"images/EdgeWise120x90.gif\" width=\"120\" height=\"90\"
border=\"0\"></a></td></tr><tr><td><a href=\"http://www.racersales.com/\"
target=\"_blank\"><img src=\"images/racestock2.gif\" width=\"120\"
height=\"90\" border=\"0\"></a></td></tr> "

I would assume IE chokes on the `tbody' element being replaced by the value
of `s'; MSHTML is known to be peculiar about tables and the `tbody' element
object in the DOM. However, prepending "<tbody>" and appending "</tbody>"
to the value of `s' caused the same error message ("unknown runtime error").
So ISTM the best solution would be to avoid `innerHTML', as often stated here.


PointedEars

P.S.: Don't you have a real name?
 
M

My Pet Programmer

Thomas 'PointedEars' Lahn said:
"Immediate" evaluation in the MSSD shows that the object reference is valid
at this point and

document.getElementById("buttonAdTable").innerHTML

yields

"<TBODY></TBODY>"

Since

s

yields
[snip] "

I would assume IE chokes on the `tbody' element being replaced by the value
of `s'; MSHTML is known to be peculiar about tables and the `tbody' element
object in the DOM. However, prepending "<tbody>" and appending "</tbody>"
to the value of `s' caused the same error message ("unknown runtime error").
So ISTM the best solution would be to avoid `innerHTML', as often stated here.

Ok, so that makes sense. I haven't been here long enough to see anything
on the InnerHTML stuff, I'll look it up and do my reading, promise.
PointedEars

P.S.: Don't you have a real name?

Of course I do, I think most people do, with the occasional rare
exception. So rare that I don't think I have ever met someone that
didn't have a name. Short answer is yes I do.

~A!
 
D

David Mark

Thomas 'PointedEars' Lahn said:




"Immediate" evaluation in the MSSD shows that the object reference is valid
at this point and
  document.getElementById("buttonAdTable").innerHTML

  "<TBODY></TBODY>"

  s

[snip]          "
I would assume IE chokes on the `tbody' element being replaced by the value
of `s'; MSHTML is known to be peculiar about tables and the `tbody' element
object in the DOM.  However, prepending "<tbody>" and appending "</tbody>"
to the value of `s' caused the same error message ("unknown runtime error").
 So ISTM the best solution would be to avoid `innerHTML', as often stated here.

Ok, so that makes sense. I haven't been here long enough to see anything
on the InnerHTML stuff, I'll look it up and do my reading, promise.

IIRC, setting the innerHTML of a table is not allowed in IE. As
mentioned, innerHTML is best avoided, as are tables for layout. This
looks like the collision of two bad practices.
 
K

Kiwi_In_VT

IIRC, setting the innerHTML of a table is not allowed in IE. As
mentioned, innerHTML is best avoided, as are tables for layout. This
looks like the collision of two bad practices.

Thanks everyone for your help.

Since I've already proven myself to be a newbie . . . here's anpother
silly question:

what is the replacement for innerHTML and for tables?
 
D

David Mark

Thanks everyone for your help.

Since I've already proven myself to be a newbie . . . here's anpother
silly question:

what is the replacement for innerHTML and for tables?

Since you are using Ajax to download HTML, rather than XML, you are
pretty much stuck with innerHTML (unless you want to redesign the
entire application.) So either redo your layout without tables or set
the innerHTML of individual table cells (as opposed to setting the
entire table at once.)
 
D

Dr J R Stockton

In comp.lang.javascript message <82ba4fde-8450-410c-b051-7f6ecf161665@p6
9g2000hsa.googlegroups.com>, Mon, 24 Dec 2007 05:37:09, Kiwi_In_VT
function randOrd(){
return (Math.round(Math.random())-0.5);
}

You don't need to round there.
smallAds.sort(randOrd); // randomize for next call

The ISO and IEC standards require the argument of .sort() to be a
determinate function. Accordingly, there is no guarantee that your
..sort(randOrd) will be satisfactory in all engines.

Shuffling code using what Knuth recommends need only be about 50%
longer, executed about 50% faster, and should be considered more
professional.

You could have discovered that, and the Knuth algorithm, by seeking
"Shuffle" in the newsgroup FAQ. The Knuth algorithm is guaranteed to
give a perfect even random distribution (if Math.random() is perfect; it
isn't, but its likely defects (except in old Opera) are unlikely to
matter in this sort of application).
 
T

Thomas 'PointedEars' Lahn

Kiwi_In_VT said:
IIRC, setting the innerHTML of a table is not allowed in IE. As
mentioned, innerHTML is best avoided, as are tables for layout. This
looks like the collision of two bad practices.

[...] here's anpother silly question: [...]
what is the replacement for innerHTML

Good question! There is no equivalent property available. You can build
the DOM subtree node by node, possibly by implementing your own parser, but
that might not be practical here. You could generate the entire table with
`innerHTML', though.
and for tables?

Tables can and should be replaced with (CSS-)positioned block-level elements
(depending on the content, e.g. `div', `ul', and `ol') if, and only if, they
are only used for presentation. Whenever a table conveys tabular
information, that is data associated with other data (say, one table cell
contains the column or row heading, and others contain the corresponding
information, it should not be replaced with anything else. This is rather a
general Web design issue and therefore should be discussed in
comp.infosystems.www.authoring.misc.


Please do not remove the attribution line that Google Groups includes
automatically. (You could trim it a bit so that superfluous information
like the sender address from the precursor (which GG replaces anyway) is not
included.)


PointedEars
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>, Mon,
Please do not remove the attribution line that Google Groups includes
automatically. (You could trim it a bit so that superfluous information
like the sender address from the precursor (which GG replaces anyway) is not
included.)

Recommended Usenet practice is to give a fully informative attribution.

Your insistence on demanding cutting it down to some peculiar local
preference is evidence of your defective personality.
 
T

Thomas 'PointedEars' Lahn

Dr said:
[...] Thomas 'PointedEars' Lahn [...] posted:
Recommended Usenet practice is to give a fully informative attribution.

You will have to prove that.
Your insistence on demanding cutting it down to some peculiar local
preference is evidence of your defective personality.

Only a troll would intentionally misunderstand the wording "You could trim
it a bit" as a demand.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top