...

View Full Version : error when refreshing page



IPfreely
01-07-2004, 03:24 PM
hey guys!

this looks like a cool forum! ok, i wrote a little script that will flip through random quotes and display them in a table every few seconds. problem is the first time the page is loaded it give an error. also, when i refresh the page, the code gives an error.

here is the code:
var CurrentMsg;//-> had original message in here

function update(msg)
{
CurrentMsg = msg;
document.getElementById("customerQuote").innerHTML=CurrentMsg;
clearTimeout(timer);
timer = setTimeout("idleMsg()",6500);
}
function update2(msg)
{
CurrentMsg = msg;
document.getElementById("custName").innerHTML=CurrentMsg;
clearTimeout(timer);
timer = setTimeout("idleMsg()",6500);
}

var notice_num = 4; // -> change this to length of array

var notices = new Array();
notices[1] = 'We\'ve used Superior Snow Remov...excellent.';
notices[2] = '"I need my shopping center cleared ..."';
notices[3] = '"We\'ve used Superior for 4 years now and they\'re an excellent service. ...."';
notices[4] = '"Superior does an excellent job handling ..."';

var customers = new Array();
customers[1] = "Alvin Trumbone<br> Aldi Inc. <br> #54, 46th & Market Philadelphia, PA.";
customers[2] = "Frank Ullman<br> Brentway Management LLC ";
customers[3] = "Barbara Corvo<br> Dion-Segal Associates";
customers[4] = "Barbara Claffie<br> Stein Investment Group";

var timer = setTimeout('idleMsg()',0); // time before first message appears

function idleMsg()
{

index = 1 + Math.round((notice_num * Math.random()));
if(index>notice_num)
{
index=notice_num;
}
if(index<1)
{
index=1;
}
update(notices[index]);
update2(customers[index]);
}

any ideas why this is buggy?? I've been testing in IE 6 on winXP

thanks for reading this post,
IPfreely

Roy Sinclair
01-07-2004, 04:03 PM
Look at this line and count the quote marks:



notices[1] = 'We've used Superior Snow Remov...excellent.';


You've got to remember to escape those embedded quotes so they don't prematurely terminate your string.

IPfreely
01-07-2004, 04:12 PM
hey roy!

thank you for looking - in my code those are actually escaped i guess when posting in a forum those slashes are stripped out or something. After more investigation I now see the error is:

Error:'document.getElementById(...)' is null or not an object

what does that mean?


thanks,
IPfreely

liorean
01-07-2004, 04:16 PM
It means that it does not find an element with the id you send to that function in the document tree.

IPfreely
01-07-2004, 04:38 PM
hmm odd.. i labeled the spans respectively: id="customerQuote" and id="custName" and it works sometimes.

Is there a difference between using document.getElementById and document.getElementByName??
arg this is frustrating!

thank you,
IPfreely

liorean
01-07-2004, 04:57 PM
Don't even think of using getElementsByName - it's the single DOM1 functionality that does not work as it should in ANY browser. All of them are buggy in their own particular way, of course.

No, that is not likely to be the problem. First, check the case of the ids again. If it matches, your problem is likely one of timing. The DOM functions must be used after the document has been loaded, they do not work while loading.

IPfreely
01-07-2004, 05:24 PM
nuts i think you are right - i don't know how to make sure that script loads after the page loads. if you look at the script you'll see that:

var timer = setTimeout('idleMsg()',0);

is what sets everything going and i'm not sure how to make sure the page loads before calling that line

thanks guys, :)

IPfreely

Roy Sinclair
01-07-2004, 05:27 PM
Just move that line to a point just before the </body> tag on your page. That'll probably require it's own <script></script> tags but it'll also make sure that everything the script references will be in existance by then.

I would also increase that number from 0 to something like 1000 since the zero value there means run it right away which makes calling using setTimeout a waste, 1000 would cause it to wait one second (1000 milliseconds).

liorean
01-07-2004, 05:36 PM
The best solution would be to use the onload event to set the timeout, if you at all need the timeout.

IPfreely
01-07-2004, 05:47 PM
wow i'm a moron - i'm going to try that :D
- let you know


thanks!
IPfreely

IPfreely
01-07-2004, 08:21 PM
damn tried it a hundred ways and won't work - i think i'm going to redo this all in flash

thanks for your help !!
IPfreely



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum