Results 1 to 3 of 3

Thread: How can I check if an object exists?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    London UK
    Posts
    671

    How can I check if an object exists?

    I have a linked js file which I use in several pages, some of these contain a ListBox Called lstNav, Others don't. If I reference lstNav in the pages where it doesn't exist I obviously get an error.

    I have tried Checking if (lstNav == null) but get a "lstNav is undefined" error,

    Does anyone know a way to do this?

  2. #2
    Fanatic Member punkpie_uk's Avatar
    Join Date
    Sep 2001
    Location
    UK
    Posts
    645
    Hi,

    I use this to check if an object exists within in a form. It displays 'doesnt exist' but displays 'does exist' if I add <input type="text" name="object1"> to the form


    Code:
    <html>
    <head>
    <title>Untitled</title>
    <script language="JavaScript" type="text/javascript">
    <!--
    function checkIfExists(){
      if (document.form1.object1){
         alert ('does exist');
      }else{
         alert ('doesnt exist');
      }
    }
    //-->
    </script>
    
    </head>
    <body>
    <p onclick="javascript:checkIfExists()">Test If Exists</p>
    <form name="form1">
    
    </form>
    </body>
    </html>
    SPREAD THE WORD!!! Are You Lee McCormick? Because I Am



    Lee M McCormick
    [email protected]

    Lee McCormick.com - Live
    Dynamically Webbed.com - In development but live

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    London UK
    Posts
    671
    Thanks very much, you wouldn't believe how much time I wasted on that one!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width