473,399 Members | 3,302 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,399 software developers and data experts.

Page refresh throws JavaScript error

(I'm rather inexperienced with JavaScript; the failing page was cobbled
together from multiple sources)...

http://pages.prodigy.net/chris_beall/STL/Timeline.html

If JavaScript is not present, or CSS 2 isn't supported, the page just
shows a complete list of dated entries. That's correct.

If JavaScript is present and CSS 2 is supported, a subset of the list is
shown (Example: 9 Jan 2008 is absent) and a button appears in the upper
right, which the user can press to see the complete list. If the button
is pressed, the complete list is shown and the button text changes,
prompting the user to press it again to hide some entries. Pressing the
button again returns things to the initial state (subset of list).
That's correct.

BUT, on Opera (and reportedly Safari), if the user REFRESHes the page,
it appears with the correct version of the button, but the complete
list, instead of the subset, is shown. That's wrong. (Some entries in
the list are hidden by changing, via script, the display property in the
last entry in the associated style sheet.)

It does not fail on Opera if accessing a local copy of the page.
It does not fail in Firefox.

On a refresh (but not when the page is first loaded) the Opera Error
Console shows:
Inline script thread
Error:
name: TypeError
message: Statement on line 23: Cannot convert undefined or null to Object
Backtrace:
Line 23 of inline#1 script in
http://pages.prodigy.net/chris_beall/STL/Timeline.html: In function changeIt
theRules[theRules.length-1].style.display = result;
Line 6 of inline#2 script in
http://pages.prodigy.net/chris_beall/STL/Timeline.html
changeIt('none');

It seems to me that either there's a browser bug here (in two browsers)
or there is something I don't know about how JavaScript is processed on
a page refresh from a remote server. I suspect the latter.

Any suggestions would be appreciated.

Thanks,
Chris Beall

Jul 22 '08 #1
6 1823
Chris Beall escribió:
Inline script thread
Error:
name: TypeError
message: Statement on line 23: Cannot convert undefined or null to Object
Backtrace:
Line 23 of inline#1 script in
http://pages.prodigy.net/chris_beall/STL/Timeline.html: In function
changeIt
theRules[theRules.length-1].style.display = result;
Line 6 of inline#2 script in
http://pages.prodigy.net/chris_beall/STL/Timeline.html
changeIt('none');
In the page you took this code from (the URL is inside a comment), this
is what they say:

"Unfortunately browser incompatibilities are so severe that this script
isn't really usable in practice yet."

You may find it easier to loop all your <li>'s and add or remove a
special class, something like "hide":

..hide{
display: none;
}

Use getElementsByClassName() to find tags with "action" class:

http://www.robertnyman.com/2008/05/2...ame-anno-2008/
And then use addClass() and removeClass() to show or hide:

http://dean.edwards.name/IE7/caveats/
I see nothing wrong in your current approach but you'd need a
cross-browser script to do so and I can't help you with it.

--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
Jul 22 '08 #2
pr
Chris Beall wrote:
>
http://pages.prodigy.net/chris_beall/STL/Timeline.html
[...]
On a refresh (but not when the page is first loaded) the Opera Error
Console shows:
Inline script thread
Error:
name: TypeError
message: Statement on line 23: Cannot convert undefined or null to Object
Backtrace:
Line 23 of inline#1 script in
http://pages.prodigy.net/chris_beall/STL/Timeline.html: In function
changeIt
theRules[theRules.length-1].style.display = result;
Line 6 of inline#2 script in
http://pages.prodigy.net/chris_beall/STL/Timeline.html
changeIt('none');

It seems to me that either there's a browser bug here (in two browsers)
or there is something I don't know about how JavaScript is processed on
a page refresh from a remote server. I suspect the latter.
Neither; you are simply calling the function before the page has fully
loaded, with predictably unpredictable results.

In this case, document.styleSheets[0].cssRules.length alternates between
zero and 22 (in Opera 9.51), the former case making a nonsense of the line

theRules[theRules.length-1].style.display = result;

since theRules[-1] doesn't exist.

Put your call to changeIt() in body onload. As security, check that
theRules.length 0 before attempting to use its contents.
Jul 22 '08 #3
pr wrote:
Chris Beall wrote:
>>
http://pages.prodigy.net/chris_beall/STL/Timeline.html
[...]
>On a refresh (but not when the page is first loaded) the Opera Error
Console shows:
Inline script thread
Error:
name: TypeError
message: Statement on line 23: Cannot convert undefined or null to
Object
Backtrace:
Line 23 of inline#1 script in
http://pages.prodigy.net/chris_beall/STL/Timeline.html: In function
changeIt
theRules[theRules.length-1].style.display = result;
Line 6 of inline#2 script in
http://pages.prodigy.net/chris_beall/STL/Timeline.html
changeIt('none');

It seems to me that either there's a browser bug here (in two
browsers) or there is something I don't know about how JavaScript is
processed on a page refresh from a remote server. I suspect the latter.

Neither; you are simply calling the function before the page has fully
loaded, with predictably unpredictable results.

In this case, document.styleSheets[0].cssRules.length alternates between
zero and 22 (in Opera 9.51), the former case making a nonsense of the line

theRules[theRules.length-1].style.display = result;

since theRules[-1] doesn't exist.

Put your call to changeIt() in body onload. As security, check that
theRules.length 0 before attempting to use its contents.

pr,

Thanks, I suspected something like that. So, although both my
JavaScript functions and the <linkto the stylesheet are within the
<headof the page, there is no guarantee that they will be completely
processed before the contents of <body>. So it's a race condition and
sometimes I win and sometimes I lose.

If I understand your suggestion, using onload guarantees that the onload
function will not be kicked off until everything else has stabilized, as
loosely documented at
http://www.w3.org/TR/html401/interac...ml#adef-onload

Also, how did you determine the (varying) value of
document.styleSheets[0].cssRules.length? I'd probably add a statement
to display it via a document.write, but perhaps there's another way that
I should know about.

Many thanks,
Chris Beall
Jul 22 '08 #4
pr
Chris Beall wrote:
If I understand your suggestion, using onload guarantees that the onload
function will not be kicked off until everything else has stabilized, as
loosely documented at
http://www.w3.org/TR/html401/interac...ml#adef-onload
Putting it generally, yes. The important thing is that putting the call
in body onload guarantees that DOM collections like document.images and
document.stylesheets have been populated and that methods you expect to
work across all the nodes in a document, like getElementsByTagName() are
in a position to do so.
>
Also, how did you determine the (varying) value of
document.styleSheets[0].cssRules.length? I'd probably add a statement
to display it via a document.write, but perhaps there's another way that
I should know about.
Just the trusty window.alert():

if (document.styleSheets[0].cssRules){
theRules = document.styleSheets[0].cssRules
alert("cssRules " + document.styleSheets[0].cssRules.length);
}

You'll be using that a lot :-)
Jul 22 '08 #5
On Jul 22, 4:18 pm, pr wrote:
Chris Beall wrote:
<snip>
>Also, how did you determine the (varying) value of
document.styleSheets[0].cssRules.length? ...
<snip>
Just the trusty window.alert():

if (document.styleSheets[0].cssRules){
theRules = document.styleSheets[0].cssRules
alert("cssRules " + document.styleSheets[0].cssRules.length);
}

You'll be using that a lot :-)
This is true, but the drawbacks of using - alert - for testing/
reporting/debugging could be mentioned. They can tell you the state at
the point when they open but opening the alert has consequences. It
blocks the execution of the code that opens it, but in doing that
frees to browser to do other things (so race conditions where a script
is racing the browser will be radically altered).

Also, they take the input focus, and so events such as onfocus, onblur
and onchnage may be triggered wherever the focus had been when the -
alert - opened. Making - alerts - problematic when examining
interactions. This problem is quite well illustrated by this page by
John Resig:-

<URL: http://ejohn.org/blog/most-bizarre-ie-quirk >

- where totally bogus conclusions have been drawn as a result of
attempting to examine features of browser/mouse interactions using an
- alert -; Replacing his test code with:-

var count = 0;
setInterval(function(){
window.status = ++count;
}, -1);

- makes an obvious nonsense of all of the conclusions drawn.

Non-blocking reporting that does not take focus used to be commonly
achieved by writing the - window.status - property, which resulted in
the written message appearing in the browser's status bar. Modern
browsers increasingly default to not allowing status bar writing by
scripts, and so need it to be enabled by the user if that technique is
to be used.

However, modern browsers (at least while using an HTML DOM) also allow
writing the innerHTML of elements, giving another easy option for non-
blocking reporting. Though more use while learning and formal testing
than in debugging a 'finished' project (as adding an element to write
into may not be practical or consequence free).
Jul 22 '08 #6
On 22 jul, 18:26, Henry <rcornf...@raindrop.co.ukwrote:
>
Non-blocking reporting that does not take focus used to be commonly
achieved by writing the - window.status - property, which resulted in
the written message appearing in the browser's status bar. Modern
browsers increasingly default to not allowing status bar writing by
scripts, and so need it to be enabled by the user if that technique is
to be used.

However, modern browsers (at least while using an HTML DOM) also allow
writing the innerHTML of elements, giving another easy option for non-
blocking reporting. Though more use while learning and formal testing
than in debugging a 'finished' project (as adding an element to write
into may not be practical or consequence free).
javascript:document.title= "now debugging";

--Jorge.
Jul 24 '08 #7

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

Similar topics

12
by: Andrew Chalk | last post by:
In a Python script running under CGI, can I programatically redirect the program to another page. Assume that I have a static HTML page that I want displayed (e.g. index.htm). Other than 'print...
18
by: Alan Z. Scharf | last post by:
1. I have a chain of six asynch callbacks initiated by a button, and want the page to refresh at the end of each callback to display A. Results of a SQLServer query showing cumulative running...
8
by: Judy Ward | last post by:
I have an index.aspx with frames. The top frame has a navigation bar with a "Login" hyperlink. If the user has already logged in I want this link to change to "Logout". I am using forms-based...
4
by: Mike | last post by:
On my web app I allow the user to enable or disable the refreshing of the page. I notcied that if the browser window is minimized the page does not refresh, or if it sits a long time without any...
4
by: Dica | last post by:
i apologize for what is no doubt a very rudimentary question, but i'm still trying to wrap my brain around .net coding habits. in classic asp, if i wanted to show search results, i'd just post the...
10
by: paulie | last post by:
Hi, I have been experiencing an issue when trying to use AJAX to reload a DIV area using a timer of 2000ms, which contains a html page with another DIV and javascript. Scenario -------------...
5
by: =?Utf-8?B?Sm9obg==?= | last post by:
Hi, I used the following code to refresh the parent page, and it works very well (Thanks to Peter Bromberg "). Response.Write("<script language='javascript' type='text/javascript'{...
1
by: raghuvendra | last post by:
Hi I have a jsp page with 4 columns: namely Category name , Category order, Input field and a submit button. All these are aligned in a row. And Each Category Name has its corresponding Category...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.