|
|||||||
New to SitePoint Forums? Register here for free!
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Nov 2005
Location: The Netherlands
Posts: 808
|
getElementById : IE gives warning when element with ID is not present
During practically every project I do I'm facing the same problem at some point.
Picture this, I need to fetch an element with where "foobar" is its ID, to change its style: Code:
var el = document.getElementById('foobar');
el.style.display = 'block';
But say, my page is a dynamic one, fetching content based on GET-variables. Say my foobar-element disappears from the document, due to GET-variables being set differently. Of course, Javascript can't find the element. Firefox handles this pretty well by failing silently in the background. IE on the other hand, shows an ugly warning message to my poor users, that el doesn't has a style-property! The workaround: Code:
if (document.getElementById('foobar') {
var el = document.getElementById('foobar');
el.style.display = 'block';
}
Is there a quick solution to this problem? |
|
|
|
|
|
#2 |
|
SitePoint Wizard
![]() ![]() Join Date: Nov 2004
Location: Åsnorrbodarna
Posts: 11,777
|
I don't think that's stupid (except for the typo
).It would be much more stupid to try to assign a property to something that is undefined. In any professional development environment, you need to do error checking. Verify that the object exists before trying to do anything with it. If you do this a lot, why not make a function out of it? Code:
function displayAsBlock(id)
{
var el = document.getElementById(id);
if (el) el.style.display = "block";
}
displayAsBlock("foobar");
displayAsBlock("snafu");
|
|
|
|
|
|
#3 | |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Nov 2005
Location: The Netherlands
Posts: 808
|
Quote:
![]() And what I actually meant with "too often" was the situation. It's not about the effect, it's about how I get there. |
|
|
|
|
|
|
#4 | |
|
SitePoint Wizard
![]() ![]() Join Date: Nov 2004
Location: Åsnorrbodarna
Posts: 11,777
|
Quote:
Code:
try {
document.getElementById("foobar").style.display = "block";
document.getElementById("snafu").style.display = "block";
...
} catch (e) {
if (e.name != "TypeError") throw e;
}
![]() |
|
|
|
|
|
|
#5 | |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Nov 2005
Location: The Netherlands
Posts: 808
|
Quote:
![]() |
|
|
|
|
|
|
#6 | |
|
CSS & JS/DOM Adept
![]() Join Date: Mar 2005
Location: USA
Posts: 5,479
|
Quote:
|
|
|
|
|
|
|
#7 |
|
Programming Since 1978
![]() ![]() ![]() ![]() Join Date: Sep 2005
Location: Sydney, NSW, Australia
Posts: 10,811
|
Opera usually sticks the error message in the Console and terminates the script at that point and you have to open the console to find the error.
What about this for a shorter version that combines the assignment (hence single equals) into the if test allowing the whole thing to be done on one line: if (el = document.getElementById('foobar') el.style.display = 'block'; |
|
|
|
|
|
#8 | |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Nov 2005
Location: The Netherlands
Posts: 808
|
Quote:
![]() Nice one, gonna remember this one. |
|
|
|
|
|
|
#9 | |
|
SitePoint Addict
![]() ![]() ![]() Join Date: Oct 2004
Location: UK
Posts: 353
|
Quote:
Code:
if (el = document.getElementById('foobar')) el.style.display = 'block';
|
|
|
|
|
|
|
#10 |
|
d^_^b
![]() ![]() ![]() Join Date: Nov 2005
Location: Gloucester, UK
Posts: 262
|
If you're using the line:
Code:
var el = document.getElementById('foobar');
Code:
if (el) el.style.display = 'block'; |
|
|
|
|
|
#11 | |
|
Programming Since 1978
![]() ![]() ![]() ![]() Join Date: Sep 2005
Location: Sydney, NSW, Australia
Posts: 10,811
|
Quote:
|
|
|
|
|
|
|
#12 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Apr 2004
Location: germany
Posts: 4,321
|
Code:
$ = function (id) {
return document.getElementById(id);
}
//
$('foobar') && $('foobar').style.display = 'block';
|
|
|
|
|
|
#13 |
|
d^_^b
![]() ![]() ![]() Join Date: Nov 2005
Location: Gloucester, UK
Posts: 262
|
True, but I've never seen your example in the wild except where someone has mistyped their comparison operator as an assignment and browsers will throw warnings about it. The example I used is more usual.
|
|
|
|
|
|
#14 |
|
SitePoint Addict
![]() ![]() ![]() Join Date: Oct 2004
Location: UK
Posts: 353
|
Felgall's tends to be more popular amongst the higher echelons of JS gurus (in my experience)
I've seen it all over, especially where brevity and elegance of coding are key (Mike Foster's code, for example, is filled with similar instances) |
|
|
|
|
|
#15 |
|
d^_^b
![]() ![]() ![]() Join Date: Nov 2005
Location: Gloucester, UK
Posts: 262
|
You learn something new every day.
![]() |
|
|
|
![]() |
| Bookmarks |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
|
All times are GMT -7. The time now is 14:53.







).






Hybrid Mode
