473,231 Members | 2,268 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,231 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 3632
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
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.