473,719 Members | 2,892 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

error: object required when declaring variable

I don't know if I'm doing this correctly. I have a little programming
experience in python, c++ and some others but this is my first time with
javascript. I'm trying have my website detect the user's browser and
assign a variable with different items if it's Internet Explorer. My
code works with firefox, opera and others but not IE.

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitl ed Document</title>

<script language="JavaS cript" type="text/javascript"><!--
browser_version = parseInt(naviga tor.appVersion) ;
browser_type = navigator.appNa me;
//var sizex,sizey
if (browser_type == "Microsoft Internet Explorer") {
sizex=document. body.clientWidt h
sizey=document. body.clientHeig ht;
}
else {
sizex=window.ou terWidth-window.innerWid th
sizey=window.ou terHeight-window.innerHei ght;
}
alert ("width = " + sizex + "height = " + sizey)
// -->
</script>
</head>

There it is, I get the error on:(line 10)sizex=docume nt.body.clientW idth

Thanks for any help.

FF
Jul 23 '05 #1
5 3086
fred wrote:
I don't know if I'm doing this correctly. I have a little
programming experience in python, c++ and some others but
this is my first time with javascript. I'm trying have
my website detect the user's browser
Don't ever try to detect the user's browsers. If you want to match
script behaviour with the DOM of the environment in which it is
executing use feature detection instead (see the FAQ, particularly <URL:
http://jibbering.com/faq/#FAQ4_26 > and related links).
and assign a variable with different items if it's Internet
Explorer. My code works with firefox, opera and others but
not IE.
It only works in Opera and Firefox as you have them configured (and
given a fairly vague definition of "works").
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1"> <title>Untitl ed Document</title>

<script language="JavaS cript" type="text/javascript"><!-- ^^^^^^^^ ^^^^
The language attribute is deprecated and rendered redundant by the use
of the required type attribute.

HTML style comments are not needed inside script elements and should be
omitted/avoided. (You don't by any chance have a really out of date book
on browser scripting?)
browser_version = parseInt(naviga tor.appVersion) ;
browser_type = navigator.appNa me;
//var sizex,sizey
If you are going to use global variables in javascript it is best
practice to explicitly declare them globally.
if (browser_type == "Microsoft Internet Explorer") {
sizex=document. body.clientWidt h
sizey=document. body.clientHeig ht;
The conditions in which any given browser will return "Microsoft
Internet Explorer" from the - navigator.appNa me - property are unrelated
to the type of browser in use, and it don't relate to the
appropriateness of reading the - document.body.c lientHeight - to acquire
any form of dimensions, even when the browser is IE. (See:- <URL:
http://jibbering.com/faq/#FAQ4_9 > and recent newsgroup threads related
to the subject)
}
else {
sizex=window.ou terWidth-window.innerWid th
sizey=window.ou terHeight-window.innerHei ght;
In principle the difference between outerWidth and innerWidth would be
the width of the window's chrome (totalled left and right) and very
different information from anything that could be acquired through
clientWidth.
}
alert ("width = " + sizex + "height = " + sizey)
// -->
</script>
</head>

There it is, I get the error on:(line
10)sizex=docume nt.body.clientW idth

^^^^^^^^^^^^^
The document.body property is not populated until the BODY element is
created (at the soonest) and the BODY element cannot be created until
the HTML parser sees (or implies) the opening body tag. Script executed
inline within the HEAD will have finished prior to the browser
encountering the opening BODY tag in the HTML source.

Richard.
Jul 23 '05 #2
What Richard said. Here is a viewport tester that tells you what
properties work for a the user agent it is loaded into.

This has been posted before, but I've done a little extra work and
think this is a better version.

Have fun, Fred.

<html><head>
<title>Viewpo rt Tester</title>
<meta name="author" content="Variou s">
<meta name="keywords" content="">
<meta name="descripti on" content="
This page was orignally from:

http://www.quirksmode.org/viewport/compatibility.html

and has been modified for a table layout that can be
copied and pasted from different browsers to more easily
compile a browser compatability matrix.

It also report user agent (UA) and platform, however
these are not reliable because some UA's allow the user
to sepcify what will reported, regardless of the actual UA
or platform.">

<style type="text/css">
td,th
{font-size: 14px; padding-right: 5; padding-left: 10;
border-bottom: 1px solid #999999;}
..pass
{font-size: 14px; background-color: green;
font-weight: bold; text-align: center; color: gold;}
..fail
{font-size: 14px; background-color: red;
font-weight: bold; text-align: center; color: darkblue;}
..Yes
{font-size: 14px; background-color: green;
font-weight: bold; text-align: center; color: gold;}
..No
{font-size: 14px; background-color: red;
font-weight: bold; text-align: center; color: darkblue;}
..count
{font-size: 14px; text-align: right; font-weight: bold;
padding-right: 10; padding-left: 10;}
p
{font-family: sans-serif; size: 14px;}
</style>

<script type="text/javascript">
var x = navigator;
var props = new Array(
'self.pageXOffs et',
'self.pageYOffs et',
'self.screenX',
'self.screenY',
'self.innerHeig ht',
'self.innerWidt h',
'self.outerHeig ht',
'self.outerWidt h',
'self.screen.he ight',
'self.screen.wi dth',
'self.screen.av ailHeight',
'self.screen.av ailWidth',
'self.screen.av ailTop',
'self.screen.av ailLeft',
'self.screen.To p',
'self.screen.Le ft',
'self.screenTop ',
'self.screenLef t',
'self.screen.co lorDepth',
'self.screen.pi xelDepth',
'document.body. clientHeight',
'document.body. clientWidth',
'document.body. scrollHeight',
'document.body. scrollWidth',
'document.body. scrollLeft',
'document.body. scrollTop',
'document.body. offsetHeight',
'document.body. offsetWidth',
'document.body. offsetTop',
'document.body. offsetLeft'
);
function uaDetail() {
var p = '<table border=\"0"\>'
+ '<tr><th style=\"text-align: left;\">Propert y</th>'
+ '<th style=\"text-align: left;\">Value</th</tr>';
if (navigator) {
var x = navigator;
p += '<tr><td>CodeNa me</td><td>' + x.appCodeName + '&nbsp;</td></tr>'
p += '<tr><td>MinorV ersion</td><td>' + x.appMinorVersi on +
'&nbsp;</td></tr>'
p +='<tr><td>Name </td><td>' + x.appName + '&nbsp;</td></tr>'
p +='<tr><td>Vers ion</td><td>' + x.appVersion + '&nbsp;</td></tr>'
p +='<tr><td>Cook ieEnabled</td><td>' + x.cookieEnabled +
'&nbsp;</td></tr>'
p +='<tr><td>CPUC lass</td><td>' + x.cpuClass + '&nbsp;</td></tr>'
p +='<tr><td>OnLi ne</td><td>' + x.onLine + '&nbsp;</td></tr>'
p +='<tr><td>Plat form</td><td>' + x.platform + '&nbsp;</td></tr>'
p +='<tr><td>User Agent (UA)</td><td>' + x.userAgent +
'&nbsp;</td></tr>'
p +='<tr><td>Brow serLanguage</td><td>' + x.browserLangua ge +
'&nbsp;</td></tr>'
p +='<tr><td>Syst emLanguage</td><td>' + x.systemLanguag e +
'&nbsp;</td></tr>'
p +='<tr><td>User Language</td><td>' + x.userLanguage + '&nbsp;</td></tr>'
p +='</table>'
} else {
p = 'Your user agent does not support the navigator object,'
+ '\nso I can\'t determine your viewport properties';
}
return p;
}
function viewportDetail( ) {
var count = 0;
var printstring = '';
var c = '';
printstring += '<table border=\"0\"><t r>'
+ '<th>#</th>'
+ '<th style=\"text-align: left;\">Propert y</th>'
+ '<th>Supported? <br>y/n</th>'
+ '<th>Value</th>'
+ '</tr>';
for (var i=0;i<props.len gth;i++) {
if (!self.screen && props[i].indexOf('self. screen') != -1) continue;
if (!document.body && props[i].indexOf('docum ent.body') != -1)
continue;

(eval(props[i]) || eval(props[i]) == 0 ) ? c = 'Yes' : c = 'No';
++count;
printstring +=
'<tr><td class=\"count\" >' + count
+ '</td><td>' + props[i]
+ '</td><td class=\"' + c + '\">' + c
+ '</td><td>' + eval(props[i])
+ '</td></tr>';
if (props[i].indexOf('docum ent.body') != -1) {
var end = props[i].substring(prop s[i].lastIndexOf('. ')+1);
newprop = 'document.docum entElement.' + end;
if (eval(newprop)) {
(eval(newprop)) ? c = 'Yes' : c = 'No';
++count;
printstring +=
'<tr><td class=\"count\" >' + count
+ '</td><td>' + newprop
+ '</td><td class=\"' + c + '\">' + c
+ '</td><td>' + eval(newprop)
+ '</td></tr>';
} } }
printstring += '</table>'
return printstring;
}
function getUA() {
if (!document.getE lementById) return;
document.getEle mentById('uaDet ail').innerHTML = getUA();
}
function reGet() {
if (!document.getE lementById) return;
document.getEle mentById('viewp ortDetail').inn erHTML = viewportDetail( );
}
</script>
</head>
<body>
<h4>Your user agent's properties<sup> <a href="#note">1</a></h4>
<pre id="uaDetail">
<script type="text/javascript">
document.write( uaDetail());
</script>
</pre>
<h4>These are the properties that work in your browser:</h4>
<pre id="viewportDet ail">
<script type="text/javascript">reG et();</script>
</pre>
<p>(<a href="#" onclick="reGet( ); return false">Refresh</a> list)</p>
<p><a name="note">The user agent values are as reported by your user
agent (browser). Depending upon the UA you are using, you may be able
to change these in a preference setting. Some browsers pretend to
be some other browser by default - please check your UA's settings
to determine if the values are accurate.</a></p>
<div style="margin-left: 50;"><p id="zz" style="padding-left: 20;"><a
href="#" onclick="
var x = document.getEle mentById('zz');
var msg = 'offsetTop: ' + x.offsetTop
+ '\noffsetLeft: ' + offsetLeft;
alert(msg);
return false;
">Set offsetTop/Left</a></p></div>

</body></html>
Jul 23 '05 #3
Fred Oz wrote:
... . Here is a viewport tester that tells you what
properties work for a the user agent it is loaded into.
That isn't strictly true. The script lists a number of properties of the
browser but does nothing towards indicating which of them most closely
relate to the viewport/client-region of any particular browsers.

<snip> 'self.screen.he ight', <snip> 'self.screen.pi xelDepth',
No screen properties relate in any way to the viewport dimensions (it
wouldn't even be valid to assume they represented an upper limit to the
viewport dimensions).

<snip> function uaDetail() {
var p = '<table border=\"0"\>'
+ '<tr><th style=\"text-align: left;\">Propert y</th>'
+ '<th style=\"text-align: left;\">Value</th</tr>';
if (navigator) {
var x = navigator; <snip> } else {
p = 'Your user agent does not support the navigator object,'
+ '\nso I can\'t determine your viewport properties';
This is a bizarre, and slightly ludicrous, comment to be making to the
user. The browser's lack of support for a - navigator - object has
nothing to do with its ability to report viewport dimensions (either in
reality or in any of the rest of the code on this page).

<snip> if (props[i].indexOf('docum ent.body') != -1) {
var end = props[i].substring(prop s[i].lastIndexOf('. ')+1);
newprop = 'document.docum entElement.' + end;
if (eval(newprop)) {
(eval(newprop)) ? c = 'Yes' : c = 'No';

<snip>

In the event that the expression - eval(newProp) - evaluated as true
then the following conditional expression must always assign "Yes" to
the value of - c -.

The recent thread with the subject "Top warning signs of bad code?"
proposes categorising all uses of - eval - to resolve constructed dot
notation property accessors as 'bad code'. This is a specific example of
where such an abuse could be directly replaced with a bracket notation
property accessor and the - eval - use omitted.

However, the rest of the code also makes extensive use of - eval, - and
because the data has been prepared in the form of full dot notation
property accessor stings their resolution to correspo0nding objects
could not be achieved otherwise (except with the Function constructor).
But the assertion that - eval - use for this task is wrong remains valid
because it is still unnecessary to use - eval - here, it is just a
matter of designing the need for - eval - out of the code.

Directly replacing the - viewportDetail - function and the - props -
array with the following, plus the addition of some additional functions
and arrays, removes the need for - eval - from the design (though it is
not the only way of doing so):-

var objects = [
['self'],
['screen'],
['document', 'body'],
['document', 'documentElemen t']
];
function arToString(){
return this.join('.');
}
for(var c = 0;c < objects.length; c++){
objects[c].toString = arToString;
}
var props = new Array(
'pageXOffset',' pageYOffset',
'screenX','scre enY',
'innerHeight',' innerWidth',
'outerHeight',' outerWidth',
'height','width ',
'availHeight',' availWidth',
'availTop','ava ilLeft',
'Top','Left',
'screenTop','sc reenLeft',
'colorDepth','p ixelDepth',
'clientHeight', 'clientWidth',
'scrollHeight', 'scrollWidth',
'scrollLeft','s crollTop',
'offsetHeight', 'offsetWidth',
'offsetTop','of fsetLeft'
);

function getObjectWithAr ray(ar){
var o = this, c = 0;
while(
(c < ar.length)&&
(
(
(o = o[ar[c++]])&&
(
(typeof o == 'object')||
(typeof o == 'function')
)
)||
(o = null)
)
);
return o;
}

function viewportDetail( ) {
var obj, prop,count = 0;
var outAr = [
'<tr><td class=\"count\" >','',
'<\/td><td>','',
'<\/td><td>','',
'<\/td><\/tr>'
];
var printstring = ('<table border=\"0\"><t r>'
+ '<th>#<\/th>'
+ '<th style=\"text-align: left;\">Propert y<\/th>'
+ '<th>Value<\/th>'
+ '<\/tr>');
for(var c = 0;c < objects.length; c++){
obj = getObjectWithAr ray(objects[c]);
if(obj){
for(var d = 0;d < props.length;d+ +){
if(typeof obj[(prop = props[d])] == 'number'){
outAr[1] = count++;
outAr[3] = objects[c]+'.'+prop;
outAr[5] = obj[prop];
printstring += outAr.join('');
}
}
}
}
return (printstring + '<\/table>');
}

Richard.
Jul 23 '05 #4
Richard Cornford wrote:
[snip]
Directly replacing the - viewportDetail - function and the - props -
array with the following, plus the addition of some additional functions
and arrays, removes the need for - eval - from the design (though it is
not the only way of doing so):-

[snip]

Thanks Richard, your comments are gratefully received.

Regards, Fred.

Jul 23 '05 #5
Fred Oz wrote:
[...] Here is a viewport tester that tells you what
properties work for a the user agent it is loaded into.

This has been posted before, but I've done a little extra work and
think this is a better version.
[...]


People might want to try

<http://www.pointedears .de/scripts/test/ObjectInspector/nightly/latest/index>

instead (even if there is still a bug crashing IE on expanding
the "history" property subtree). Comments and bug reports not
covered by the bug list in "About" are welcome.
PointedEars
--
"In christianity neither morality nor religion
come into contact with reality at any point."
-- Friedrich Wilhelm Nietzsche
Jul 23 '05 #6

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

Similar topics

6
4749
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much appreciated. Thanks in advance
10
2354
by: Sune | last post by:
Hi, previously I used Eclipse CDT for compiling my files just to get started with C and leave C++ behind. Now it's time to get a little more serious so I've moved my files to a new workplace and begun to use GNU Autotools. I'm sorry to say I'm new to gcc as well :( Now I get the most ridiculous compile error which I'm unable to solve. Can someone, please, help me with this? gcc output together with the files mentioned in the gcc error...
3
3190
by: Jon | last post by:
I'm learning about datatables. When using the example provided by MS in the ..NET Framework Class Library for DATATABLE (see below) I get an error on line 3 that says "Type expected". Is something missing from the code? Thanks - Jon Private Sub MakeParentTable() ' Create a new DataTable. Dim myDataTable As Datatable = New Datatable("ParentTable")
7
1568
by: Brett | last post by:
I'm not sure why I keep getting this error, "Object reference not set to an instance of an object". Private Function somefunction() as string Dim MyCurrentClass As New Class1 Try For i As Integer = 0 To LinkResults.Length - 1 With MyCurrentClass.SqlCmd_Insert_LinkVB .CommandType = System.Data.CommandType.StoredProcedure
8
2471
by: ST | last post by:
Hello everyone, Can anyone help me with this error above when I debug my web app project in vstudio.net?? I can't figure it out! It was working fine for months, and now all of a sudden it's not!! This is the error: biopsy.searchsubject.btnSubject_Click(Object Sender, EventArgs e) in C:\INetPub\WWWRoot\biopsy\searchsubject.aspx.vb:198 System.Web.UI.WebControls.Button.OnClick(EventArgs e)
16
2897
by: anonymous.user0 | last post by:
The way I understand it, if I have an object Listener that has registered as a listener for some event Event that's produced by an object Emitter, as long as Emitter is still allocated Listener will stay alive. Is this correct? If this is correct, I've got a problem. Let's say I've got an object Customer that has an PurchaseList (Collection) of Purchase objects. Now, these Purchase objects were pulled from a datasource Datasource. The...
44
3377
by: petermichaux | last post by:
Hi, I have been using the following line of code to create an object called "Serious" if it doesn't already exist. if (Serious == null) {var Serious = {};} This works in the scripts I use it but but www.jslint.com is not happy with me.
2
19479
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I will be writing this article intended for those who are in the same level, or maybe lower, of my technical knowledge. I would be using layman's words, or maybe, my own words as how I understand them, hoping, you will understand it the same way that...
0
2897
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I will be writing this article intended for those who are in the same level, or maybe lower, of my technical knowledge. I would be using layman's words, or maybe, my own words as how I understand them, hoping, you will understand it the same way that...
0
8833
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9365
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9210
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9126
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6661
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5972
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4482
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3186
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2125
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.