Solved

Javascript Error when field is not on the page.

Posted on 2007-12-05
4
174 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

624 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