Solved

Javascript Error when field is not on the page.

Posted on 2007-12-05
4
167 Views
Last Modified: 2010-04-16
I have a piece of code (see below) that is supposed to hide a field on our website.  See this thread here:

http://www.experts-exchange.com/Programming/Languages/Scripting/Q_22993842.html

It works fine for the particular page that has the field on it, but all the other pages get an "Object Required" error because the script can't find it.  Is there a way to prevent this? IE: have the script check to see if the field exists or not.  I'm forced to put the script in the footer because this is a hosted page that I have limited access to.

Thanks!
<script type="text/javascript">

    document.getElementById('partner').style.display = 'none';

</script>

Open in new window

0
Comment
Question by:BasilFawlty001
  • 3
4 Comments
 
LVL 9

Expert Comment

by:william007
ID: 20412346


try this: 
 

   <script type="text/javascript">

    if(document.getElementById('partner')){

      document.getElementById('partner').style.display = 'none';

    }

   </script>
 

this is to check whether the element exists before calling the setting the display property

Open in new window

0
 
LVL 9

Accepted Solution

by:
william007 earned 500 total points
ID: 20412363
Sorry, a bit mess up, just reorganized it :)
Try the following code:
This is to check whether the element exists before calling the setting the display property
   <script type="text/javascript">

    if(document.getElementById('partner')){

      document.getElementById('partner').style.display = 'none';

    }

   </script>

Open in new window

0
 

Author Comment

by:BasilFawlty001
ID: 20412417
Thanks!
0
 
LVL 9

Expert Comment

by:william007
ID: 20412450
No problem, glad it helps :)
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now