473,325 Members | 2,712 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,325 software developers and data experts.

"Object not found" error

Tarantulus
114 100+
Expand|Select|Wrap|Line Numbers
  1. if("dept"=='dept'){
  2.     document.getElementById('add').style.display='none';
  3. };
  4.  
  5. <DIV id="add">
  6.  
  7. <some html here..>
  8.  
  9. </DIV>
this gives me an "object not found" error, any ideas what could be causing it?

(PS it might seem odd check if dept is the same as dept, but the first "dept" is actually a PHP variable)
Aug 20 '07 #1
6 3637
acoder
16,027 Expert Mod 8TB
I've changed the thread title for you. Please use a good thread title. Thanks!

Is this how your code appears? The Javascript should be separate from the HTML within script tags.
Aug 20 '07 #2
Tarantulus
114 100+
I've changed the thread title for you. Please use a good thread title. Thanks!

Is this how your code appears? The Javascript should be separate from the HTML within script tags.
My apologies.. thanks for changing the title.

I left out the script tags they encapsualte the JS and the <DIV> comes after as it well should

Expand|Select|Wrap|Line Numbers
  1. <script>
  2.  
  3. Javascript..
  4.  
  5. </script>
  6. <div>
Aug 20 '07 #3
acoder
16,027 Expert Mod 8TB
Is this Javascript within a function? If not, that may (rather, will) be your problem. It comes across the Javascript and tries to set the display property to 'none', but it hasn't come across an object with an id of "add" yet, hence the Object not found error.

To solve this, run this code on onload or after the div. Alternatively, use your PHP code to check that the PHP variable is equal to 'dept'. If it is, set the display property to none inline:
[PHP]<?php if ($phpvar == 'dept') echo "style='display:none';"; ?>[/PHP]
Aug 20 '07 #4
ronnil
134 Expert 100+
you problem is probably that you try to access your div tag before it is actually instantiated.

there are two immediate solutions to this problem

1. Set your script below the div tag. Then it should load like it's supposed to

2. Put you execution of the script inside a window load event (this is the best way)

e.g.
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <!-- header stuff here -->
  4. <script language="javascript">
  5.  
  6.  
  7. function windowOnLoad()
  8. {
  9.     var divElement = document.getElementById('myDiv');
  10.     //do stuff with your div here
  11. }
  12.  
  13. try{
  14.     window.attachEvent('onload',windowOnLoad); //MSIE
  15. } catch(e) { 
  16.     try{
  17.     window.addEventListener('load,windowOnLoad,false); //Proper browser
  18.     } catch(e) {
  19.         alert('Your browser is really old');
  20.     }
  21.  
  22. </script>
  23. </head>
  24. <body>
  25. <div id="myDiv">
  26. </div>
  27. </body>
  28. </html>
  29.  

and a little advice: try to always acces your element with document.getElementById, this ensures better browsercompability :)
Aug 20 '07 #5
Tarantulus
114 100+
DOH! of course, you're right... I'm such an idiot sometimes.

thanks peeps
Aug 20 '07 #6
acoder
16,027 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. ...<script language="javascript">
  2. function windowOnLoad()
  3. {
  4.     var divElement = document.getElementById('myDiv');
  5.     //do stuff with your div here
  6. }
  7.  
  8. try{
  9.     window.attachEvent('onload',windowOnLoad); //MSIE
  10. } catch(e) { 
  11.     try{
  12.     window.addEventListener('load,windowOnLoad,false); //Proper browser
  13.     } catch(e) {
  14.         alert('Your browser is really old');
  15.     }
  16.  
  17. </script>
  18. </head>
  19.  
Similar replies and a good example. Just one or two points though:
  • The "language" attribute of the script tag is deprecated. You should use
    Expand|Select|Wrap|Line Numbers
    1. type="text/javascript
    instead.
  • This is a personal preference and it won't really make much of a difference, but I would put the standard method first before the IE-only method.
Aug 20 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: deko | last post by:
I'm getting intermittent "Object Invalid or No Longer Set" errors in my Access 2002 mdb. What causes these errors? Has anyone dealt with this before? I can't trace it because it's not easy...
6
by: Lauchlan M | last post by:
Hi. Usin ASP.NET, getting an "Object reference not set to an instance of an object" error. In my login.aspx page I have: string arrUserRoles = new string {"UserRole"};...
1
by: Lauchlan M | last post by:
Hi. I'm using ASP.NET, getting an "Object reference not set to an instance of an object" error. In my login.aspx page I have: string arrUserRoles = new string {"UserRole"};...
4
by: mattsthompson | last post by:
Im writing a DLL that extends IHttpHandler to intercept requests for a certain file extension and deliver watermarked images. I'm using LeadTools' .NET framework for the image manipulation and it...
2
by: louie.hutzel | last post by:
This JUST started happening, I don't remember changing any code: When I click the submit button on my form, stuff is supposed to happen (which it does correctly) and a result message is posted back...
2
by: johkar | last post by:
Why does if(win==null || win.closed) return true after one PDF is open. Something very wierd going on with IE 6. I also get a member not found error if a child window is already open. This script...
0
by: piyumi80 | last post by:
hi, i wrote the following code to get a specific data row from the data set.but it generates the "Object reference not set to an instance of an object.".....error private void...
8
by: webgenius | last post by:
Whenever I click the SUBMIT button in my HTML page, I get the "Object not found!" error. It was working fine yesterday. This error started to occur after I installed Codelobster and made some changes...
3
maliksleo
by: maliksleo | last post by:
hi all i m having this "The resource cannot be found." error. I applied the url rewriting on my project every page is being browsed very well but i am getting the above error an some pages i dont...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.