Making A Show/hide Smaller
How would I make this code smaller? Maybe a toggle, but people were saying this was easily done in jQuery. But the problem is that I am not a fan of using jQuery for just one thing
Solution 1:
DRY it up.
var b='block',h='none',m='message',f='fade';
functions(i,d){document.getElementById(i).style.display=d}
functionopen(){s(m,b);s(f,b)}
functionclose(){s(m,h);s(f,h)}
With the whitespace and proper variable names (to be passed to a minifier), this looks like:
var show = 'block', hide = 'none', message = 'message', fade = 'fade';
functionsetStyle(id, display) {
document.getElementById(id).style.display=display;
}
functionopen() {
setStyle(message, show);
setStyle(fade, show);
}
functionclose() {
setStyle(message, hide);
setStyle(fade, hide);
}
There are some best-practices which don't relate to the question but are worth considering if your project grows beyond this trivial situation:
- Use a minifier. My favorite is uglifyjs. This allows you to use meaningful variable names in your unminified code (like the second example). The minifier will output code more like (but probably even better than) the first example. Even with a minifier, keep thinking about what it can and cannot do - creating a private shortcut to a long public API like document.getElementById can aid the minification if you use that API frequently. Look at the minified code to make sure there isn't something you can do to optimize it.
- Separate your javascript into .js modules that are loaded separate from the page and asychrounously, if possible.
- Manage all your static assets (like the .js modules) so they have a long cache timeout - use the
Expires:
http header. Then change their URLs when they actually change. This way, clients can cache them indefinitely until you change them & then the client will immediately fetch a new version. - Put discrete modules inside function wrappers, so that your variables don't conflict with other pieces of code - either your own or 3rd party modules. If you want to make a variable public, do it explicitly:
window.pubvar =
Solution 2:
var message = document.getElementById('message'),
fade = document.getElementById('fade');
functionopen() {
message.style.display = fade.style.display = 'block';
}
functionclose() {
message.style.display = fade.style.display = 'none';
}
Or:
functiontoggle() {
var message = document.getElementById('message'),
fade = document.getElementById('fade'),
currentdisplay = getComputedStyle(message, null)['display'];
if(currentdisplay == 'block' || currentdisplay == 'inline')
message.style.display = fade.style.display = 'none';
else
message.style.display = fade.style.display = 'block'; /* or inline */
}
Or:
functiontoggle() {
var currentdisplay = getComputedStyle(arguments[1], null)['display'],
i,
newdisplay;
if(currentdisplay == 'block' || currentdisplay == 'inline')
newdisplay = 'none';
else
newdisplay = 'block';
for(i = 0; i < arguments.length; i++)
arguments[i].style.display = newdisplay;
}
var message = document.getElementById('message'),
fade = document.getElementById('fade');
toggle(message, fade); /* hide */toggle(message, fade); /* show */document.body.onclick = function(){
toggle(message, fade);
}
Toggle Example:
Solution 3:
var toggle = function(doc){
var $ = doc.getElementById, message = $('message'), fade = $('fade'), open = true;
returnfunction(){
var display = open ? 'none' : 'block';
message.style.display = display;
fade.style.display = display;
open = !open;
}
}(document);
toggle(); // Hide both elementstoggle(); // Show both elements. Rinse and repeat.
Solution 4:
Avoids polluting global scope:
(function() {
var msgstl = document.getElementById('message').style,
fdestl = document.getElementById('fade').style;
window.open = function() {msgstl.display = fdestl.display = "block";};
window.close = function() {msgstl.display = fdestl.display = "none";};
})();
Solution 5:
One thing is you can create a helper function for setting styles on elements. This would be useful in cases where you need to set many different elements.
functionsetStyle(element, style, value) {
document.getElementById(element).style[style] = value;
}
functionopen() {
setStyle('message', 'display', 'block');
setStyle('fade', 'display', 'block');
}
functionclose() {
setStyle('message', 'display', 'none');
setStyle('fade', 'display', 'none');
}
You may also want to set the elements to variables if you work with the elements often enough.
var message = document.getElementById('message'), fade = ...
Post a Comment for "Making A Show/hide Smaller"