Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"Object expected" error - Code: 0 ...how can I resolve this?

Status
Not open for further replies.

lesj1968

Programmer
Sep 3, 2005
166
GB
Hi

I have the following simple function in a file called functions.js (which is located in a subfolder called "includes"):

function DisplayMenu(){
document.write("Test");
}


I then have an ASP file called default.asp which calls the function above as follows:

<!--#include file="includes/functions.js"-->

This is placed inbetween the <head> tags although I have tried placing it before the <html> tags and also tried placing it in the main body - it seems as though the positioning of this include statement makes no difference.


I then have the following <body> tag in default.asp:

<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" onLoad="writeMenus(), openPeeker()" onResize="if (isNS4) nsResizeHandler()">

The functions within the body tag are all also in the file functions.js.


I then call the function DisplayMenu() in the main body of default.asp:


<script type="text/javascript">

DisplayMenu()

</script>


-------------------------

Ok my problem is everything works BUT in the bottom left of the screen when default.asp is displayed in the browser there is a yellow triangle with an exclamation mark and when clicked says I have the following error:

Line: 831 (which is well beyond the last </html> tag by the way)
Char: 1
Error: Object expected
Code: 0
URL: default.asp


I have checked all the spelling and syntax and I do not think I have made any mistakes (for example missing quotes etc) so cannot see why I am getting this error. I have removed all other code in the default.asp file to narrow down the problem and it is clearly a problem related to the <body> tag I have and also the way I am calling the DisplayMenu() function.

By the way my browser is IE6 running on Windows2000, although the latter shouldn't make a difference.

I will really appreciate any help as I have been trying to solve this problem for a week now. Thank you very much in advance.

Les
 
Try moving your script calling DisplayMenu() to the bottom of the page below the </HTML> tag.
This is a test to see if perhaps the function call is occuring before the HTML is properly formed and causing the object not found error.

At the moment DisplayMenu does not seem to have anything to do with WriteMenu but I suspect it eventually will?
I think your DisplayMenu is executing prior to WriteMenu which is in the OnLoad event which will not fire until after the page load has completed.



Paranoid? ME?? WHO WANTS TO KNOW????
 
Line: 831 (which is well beyond the last </html> tag by the way)

It may well be past the closing HTML tag in your original source, but remember that the include files will increase the line count.

I suggest posting a URL to your page.

Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Comment out the DisplayMenu() line and refrsh the page. Problem still there?

Start removing the functions one by one from the onload in the body... refresh...

The idea is to isolate what part of your page is causing this error... so by "switching off" certain parts of your code, you can narrow things down.

If you are still having problems, please post a URL to the page for us to diagnose directly.

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]

What is Javascript? faq216-6094
 
Yes I agree it appears to be something to do with the order of how the writeMenus() and DisplayMenu() functions are called.

In functions.js, writeMenus() does come before DisplayMenu(), however I have inserted test alert statements to see which is actually called first in functions.js when default.asp is run. The results showed that the end of functions.js is reached first then DisplayMenu() is called and then finally writeMenus(). All seems a bit odd. Am I write in assuming the function DisplayMenu() is placed in the wrong order in functions.js?

DisplayMenu() is only actually used so all code following it in functions.js is carried out. I was previously having problems without this function. writeMenus() on it's own is not adequate - hence I need both, which does now work fine - however I get this Object expected error message, which doesn't interfere with the running of the code or output but is something I would want sorted out.

Thank you for any help on this.
 
Maybe it's something you could call from within writeMenus() or appeand to the end of the onload in the body tag. It could be related to requiring a complete DOM.

btw... have you validated your HTML against the DOCTYPE you are using? It could just be a simple HTML problem!

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]

What is Javascript? faq216-6094
 
Hi Jeff

I'll try what you suggest thanks. Not sure what you mean when you mention validating my html agains the doctype. I have tried removing all code other than the DisplayMenu() and body tag (specifically the onLoad call to writeMenus()) and it is certainly related to both of these functions. If they are removed all other html runs fine.

I've tried appending to the onload in the body tag without success. All very strange.

I'm afraid my page is on an internal web server.
 
I'm just looking at this code you posted:

Code:
onLoad="writeMenus(), openPeeker()"

You should replace the comma with a semicolon, as that is the correct statement delimiter for JavaScript.

Can you confirm that the writeMenus and openPeeker functions both exist within the scope of the calling page?

Dan




[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Not sure what you mean when you mention validating my html agains the doctype.
A web page is a structured document. You have open tags and close tags (that match the open tags) and you put them all in a certain order. As long as all the HTML markup adheres to the rules (as defined by the W3C) then your code is considered "valid" and will display and function predictably across platform and browser.

If you do not have valid markup then you could end up experiencing bizarre differences and problems that don't necessarily seem to make sense.

Visit this URL, read the pages there and look at validating your page there:
As you develop more code you will begin to appreciate Standards Compliance, validation and doctypes. It makes your life as a developer (building cross platform and cross browser solutions) a LOT more manageable.

Google on "Web Standards" "validator" "doctype"

I'm afraid my page is on an internal web server.
Don't be afraid. Find a place outside your intranet (if the content is acceptable) and host it there. I don't think we're going to get a lot further on this without seeing the code. It's like driving blind! [smile]

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]

What is Javascript? faq216-6094
 
Here is my code in default.asp (which I have removed all unnecessary lines but error still keeps occuring):


<%@ Language=VBScript %>
<% OPTION EXPLICIT %>
<!--#include file="includes/Browsercheck.asp"-->
<!--#include file="includes/Connection.asp"-->
<!--#include file="includes/functions.js"-->


<html>
<head>
<title>test menu</title>

</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" onLoad="writeMenus(), openPeeker()" onResize="if (isNS4) nsResizeHandler()">



<script language="JavaScript">

DisplayMenu()

</script>

</body>
<!--#include file="includes/connclose.asp"-->
</html>
 
the only thing we'd be able to see from what you've given us is this:

Code:
onLoad="writeMenus(), openPeeker()"
which should be
Code:
onLoad="writeMenus()[red];[/red] openPeeker()"

*cLFlaVA
----------------------------
[tt]I already made like infinity of those at scout camp...[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
When I debug the code the problem appears to be writeMenus() in the body tag. The spelling is all consistent. I've gone over all of it many times without success. I'm sure it must be something very simple.

My menu code in functions.js was submitted in one of my earlier posts today. I can post it in this post if necessary.
 
I've cracked it. I removed the openpeeker function from the body tag and it all works fine. Thank you guys for all your help. I'm not sure how I missed it but openPeeker was an old function that I had forgotten I had removed from functions.js! No wonder it was trying to get an object that didn't exist!
 
Ah good point... I didn't realise that. Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top