Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Javascript Error when field is not on the page.

Posted on 2007-12-05
4
Medium Priority
?
178 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 2000 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

705 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