PDA

View Full Version : script error: object required



wishiwasageek
06-01-2005, 03:50 AM
tnis the script in the head of my HTML doc.
it says that an object is required at line 4, char 5.
I thought it was an object?


<script type="text/javascript"><!--
var d = document;
function resizeBox(){
sObj = d.getElementById("container").style
if(d.body.clientWidth)(d.body.clientWidth>600) ? sObj.width= "600px" : sObj.width = "100%"
}
onload = resizeBox;
onresize = resizeBox;
//-->
</script>

The script is trying to do that max-width in IE thingy.

thanx
WishIwasAgeek :o

jscheuer1
06-01-2005, 04:34 AM
If there is no element with the id 'container' (spelling counts!) parsed by the browser at the time this script is executed, there is no object. Also, can you be sure which line is line 4? If it is the 5th line in the snippet (1st line could be 0) and you are using a doctype, document.body is not valid. Also, to be safe, use a line-break after the if statement. That's just off the top of my head, there could be other possibilities.

wishiwasageek
06-01-2005, 05:00 AM
sure - re. line numbers but this is the line identified in HTML-KIT (it was actually line 20 on the error message which made sense in HTML-KIT) but fine.

What about the container thingy? Didn't I spell it right? What should it say?

Sorry I don't understand what you are saying re. doctype? are you saying that this script have html rather than body in the above script?

if not what type of declaration should I be using?

thanx again
WIWAG :(

jscheuer1
06-01-2005, 05:40 AM
sObj = d.getElementById("container").styleThe above requires an element having the attribute id="container". Ex:
<div id="container">

some stuff can go here

</div>The word 'container' must be spelled exactly the same, case sensitive, in both the script and html code. In addition, said element must have been parsed by the browser before the code is executed.

About doctype - A doctype looks something like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">and (if used) is the very first thing on a page. Most doctypes invalidate IE's use of 'document.body', requiring instead the more standard 'document.documentElement' however, since some older versions of IE don't recognize doctypes, simply changing to documentElement will mess things up on them, that is another discussion. I'm not familiar with HTML-KIT but, it may assume a doctype even if none is specified.

wishiwasageek
06-01-2005, 05:52 AM
So should the text be changed to document.documentbody?
(No one is using earlier than IE 6.0)

thanx
WIWAG :o

jscheuer1
06-01-2005, 06:09 AM
That's document.documentElement (literally) and, how should I know? You have to identify wether or not this is even the problem first. If you ask a question about an isolated piece of code like that, unless there are obvious errors, all you will get will be speculation. I'm getting the feeling that you are looking for a magic bullet fix for the error. In my first response I mentioned several possibilities, all of which should be tried individually and in combination to see if any or all resolve the error. I'm also getting the impression, from your response, that you may be having difficulty holding more than one idea at once in mind while considering this issue. No way to be sure. If that is the case, it will be difficult to advise you further in this manner. Better to put a test page up where I can have a look at it.

mwinter
06-01-2005, 04:36 PM
When I covered this a while ago, there seems to have been a few misconceptions on my part, coupled with a little misreading of Microsoft's documentation.

The simple solution to this problem is to add this to your style sheet:


* html #container {
width: expression((document.body.clientWidth > 600) ? '600px' : 'auto');
}This works because by the time IE tries to apply the style to the container, it will have encountered the body element so it can read its values. This is why the documentElement property must be used if the style is being applied to the body element itself: only the html element will be accessible when the style sheet is applied.

As for the newfound ability to use strings and even concatenate them? I have no idea whatsoever as to why it didn't work when I was trying this previously. :confused:

With a bit of jiggery-pokery, it would also be possible to make your comparisons in terms of em units, but that's too much hassle for me at the moment. I have a headache. :(

Mike

wishiwasageek
06-01-2005, 11:52 PM
jscheuer1 said

That's document.documentElement (literally) and, how should I know? You have to identify wether or not this is even the problem first.

Well if your read mwinter's reply clearly this IS part of the problem.
How should you jscheuer1 know? well I don't know so how can I tell you?? You don't HAVE to reply!! I appreciate that you do however. Yes, I definitely have trouble with scripting as I am a beginner (hence student tag under my name) but you don't have to get exasperated (or maybe you do! that's cool I'm pretty frustrated too.)

Sorry to give Mwinter a headache and make you grumpy but I am a beginner and do appreciate your answers always. I could give up and just keep creating crap that works, but am trying to do the right thing here and learn to code things properly.

jscheuer1 said

If you ask a question about an isolated piece of code like that, unless there are obvious errors, all you will get will be speculation.

As to isolated - that's all the code there is - sorry!! I explained what it was trying to do (max width for page)

Quite seriously - how would you have asked the question that would have been more helpful? I don't want to put a page up as I don't have all the security you guys do like preventing browsing on a directory etc. (and all other guests here). Sorry Jscheuer1 - it's part of being a legendary teacher - you get exasperating students.

WIWAG :o

jscheuer1
06-02-2005, 07:06 AM
Now I'm having trouble following you. If that's all you have, then there is no object, you need an element with the id used by the script or else there is no object, as I said near the start.

wishiwasageek
06-03-2005, 03:23 AM
Going to give Mwinter's idea a go first as that seems the simplest.

Believe it or not I'm with you now - yes there would be an element with an id container in the HTML code I was just asking if there was anything glaringly obvious with the code itself before I even began to apply it to the HTML. If there isn't an element with ID code, then yeah the js wouldn't be happy.

Like I said, I'll try Mwinter's idea, but thanx for helping me understand the jscript, again it's frustrating for you, but also for me as I can't word my questions properly! I don't know enough about what I'm doing to ask the questions so you understand them. Sorry sorry sorry

WIWAG :o

mail2bansi
06-09-2005, 08:28 PM
Hi
Here is situation

I have a JSP page which has following code
<!-- Start of JSp Page-->

<SCRIPT language=JavaScript src="/PasswordManagement/js/eohhsDefault.js" type=text/javascript></SCRIPT>

<BODY text=#000000 vLink=#660099 aLink=#ff9900 link=#003399 bgColor=#ffffff
onload=initRollovers();ToggleExec();displayDate(); marginwidth="0" marginheight="0">

<jsp:include page="Header.jsp" />

<!-- End of JSp Page-->

Pl note the call to displayDate() function from BODY tag

which is defined in eohhsdefault.js as follows

function displayDate()
{
// To display greeting
date = new Date();
var day_of_week_number = date.getDay();
var day_of_month = date.getDate();
var month_number = date.getMonth();
var hour_of_day = date.getHours();
var minutes_of_day = date.getMinutes();

/* var year = date.getYear() - 100; // This code is for 1999 and before
*/
var year = date.getYear(); // This code is for the year 2000

var day_of_week = '';
var month = ''

if(month_number == 0){month = 'Jan.';}
if(month_number == 1){month = 'Feb.';}
if(month_number == 2){month = 'March';}
if(month_number == 3){month = 'April';}
if(month_number == 4){month = 'May';}
if(month_number == 5){month = 'June';}
if(month_number == 6){month = 'July';}
if(month_number == 7){month = 'Aug.';}
if(month_number == 8){month = 'Sept.';}
if(month_number == 9){month = 'Oct.';}
if(month_number == 10){month = 'Nov.';}
if(month_number == 11){month ='Dec.';}

if(day_of_week_number == 0){day_of_week = 'Sun';}
if(day_of_week_number == 1){day_of_week = 'Mon';}
if(day_of_week_number == 2){day_of_week = 'Tues';}
if(day_of_week_number == 3){day_of_week = 'Wed';}
if(day_of_week_number == 4){day_of_week = 'Thu';}
if(day_of_week_number == 5){day_of_week = 'Fri';}
if(day_of_week_number == 6){day_of_week = 'Sat';}

var appname = navigator.appName;
var appversion = parseInt(navigator.appVersion);
if (appname == "Netscape" && appversion >= 3)
{
// At this time, we want to show the date in the form: July 12, 2003.
var temp = parseInt(year);
year = temp + 1900;
// var date_to_show = day_of_week + ', ' + month + ' ' + day_of_month + ', ' + year;
var date_to_show = month + ' ' + day_of_month + ', ' + year;
}
else
{
// At this time, we want to show the date in the form: July 12, 2003.
// var date_to_show = day_of_week + ', ' + month + ' ' + day_of_month + ', ' + year;
var date_to_show = month + ' ' + day_of_month + ', ' + year;

}

var d = document.getElementById("DateLayer");
d.innerHTML = date_to_show;
}


The error is thrown from Header.jsp
<!-- display date -->
<TABLE cellSpacing=0 cellPadding=0 width=760 border=0>
<TBODY>
<TR>
<TD width=10 background=/PasswordManagement/images/date_bg.jpg>&nbsp;</TD>
<TD width=120 background=/PasswordManagement/images/date_bg.jpg>DATE
<SPAN class=DateLayer id=DateLayer></SPAN></TD>
<TD><IMG id=logo_img2 height=30 alt="" src="/PasswordManagement/images/logo_img2.jpg" width="100%" border=0 name=logo_img2></TD>
</TR>
<TR>
<TD vAlign=top width=10><IMG height=2 src="/PasswordManagement/images/blank(1).gif" width=10></TD>
</TR>
</TBODY>
</TABLE>

Sometimes error is shown at SPAN tag or at it next line. I suspect SPAN tag the error is Object required Line 60 Char 4

Your time /help is highly appreciate

mwinter
06-09-2005, 09:20 PM
I have a JSP page which has following codeWhen posing a question, it's usually better to post a URL to a demonstration. It's very easy to miss important information when showing only snippets.


<SCRIPT language=JavaScript src="/PasswordManagement/js/eohhsDefault.js" type=text/javascript></SCRIPT>Drop the language attribute and quote the type attribute value[1].


<BODY text=#000000 vLink=#660099 aLink=#ff9900 link=#003399 bgColor=#ffffff
onload=initRollovers();ToggleExec();displayDate();All of these attribute values need to be quoted. This could be the cause of your problem. If you're using CSS (and you should this side of the millenium), those colour values should be specified in the style sheet.


function displayDate()

[...]

var year = date.getYear(); // This code is for the year 2000Use the getFullYear method instead. There is no need to adjust the value returned from this method.

A more efficient approach to your code is:


function displayDate() {
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
now = new Date();

document.write(months[now.getMonth()]
+ ' ' + now.getDate()
+ ', ' + now.getFullYear());
}and include it where you want it to appear. If you must use the innerHTML property (which isn't supported on all browsers), then use feature detection first:


var element = null;

if(document.getElementById) {
element = document.getElementById('...');
}
if(element && ('string' == typeof element.innerHTML)) {
element.innerHTML = str;
}Really, more thorough testing is required, but that's better than nothing.

I really don't see the point in showing the user their local time, anyway. All operating systems provide access to the system time, and if the user's computer clock is inaccurate (and most probably are), then showing an incorrect time is hardly useful. Most people with have wrist watches or wall clocks, too.


var appname = navigator.appName;
var appversion = parseInt(navigator.appVersion);
if (appname == "Netscape" && appversion >= 3)
{[/code]Dump this. Seriously. It doesn't actually provide you with useful information, and doesn't test for the information you really want. If you couldn't use getFullYear (and you can), then installing this substitute would be better:


/* Adapted from code by Dr JR Stockton.
* Untested in its current form.
*/
Date.prototype.getFullYear = function() {
var y = Math.round(this.getTime() / 31556952000) + 1970;

return y + (this.getYear() - y) % 100;
};


var temp = parseInt(year);The variable, year, in your code is always a number, so calling parseInt is a waste of time. For future reference, you should never use parseInt without the radix argument, unless you really want it to negotiate between decimal and hexadecimal formats.

Mike


[1] All attribute values that contain characters other than just letters, numbers, underscores (_), hyphens (-), colons (:), and dots (.) must be quoted. If you can't (or don't want to bother) remembering the specifics, quote all attribute values (it never does any harm).