situational Javascript based on body ID

I know that this is not anything revolutionary or anything, but I found it very helpful when making this site, so I thought I would share.

By assigning the body element an ID, you can check it with Javascript, and then execute what you need to based on this condition, like so(wordpress example):

Some php, for the Wordpress template (in header.php):

code!

<?php
 
if(is_home()) //if we are at the wordpress "home"
	$location = 'home';
elseif(is_single() || is_page()) // if it is a single post or page
	$location = 'single';
elseif(is_some_strange_situation()) // a user function that test for something strange
	$location = 'strange';
else // otherwise, nothing special
	$location = 'default';
 
?>
<body id="<?php echo $location ?>">

Then some Javascript:

code!

<script type="text/javascript">
	switch(document.body.id){
		case 'home':
			runAtHomeOnly();
			break;
		case 'single':
			runOnSingleOnly();
			break;
		case 'strange':
			runOnStrangeOnly();
		default:
			runOtherwise();
			break;
	}
	runThisAlways();
 
	function runAtHomeOnly(){
		alert('You are home.');
	}
 
	function runOnSingleOnly(){
		alert('You are on a single post or page.');
	}
 
	function runOnStrangeOnly(){
		alert('You are somewhere strange.');
	}
 
	function runOtherwise(){
		alert('You are not home, at a single post or page, and you are not somewhere strange.');
	}
 
	function runThisAlways(){
		alert('I always run.');
	}
</script>

The example is a little verbose I know, but wanted to make sure my point was gotten ;-)

addthis

Leave a Reply

ok to use:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

bonus!

If you want to post code, you can use:

<pre lang="[language]">[code]</pre>

Where [language] is a valid geshi language type, and where [code] is your code.

its time to get tricky.

Site launched!

Just launched the site with the sexy theme you see here, inspired by lite-brite and the use of cheap effective vector brushes.

The posting here will be mainly focused on the little pieces of code i write during the course of my day. I will release code, themes and plugins here when i get around to finalizing and packaging stuff.

A couple of notes on the creation of this site, in no reasonable order:

  • Wordpress is annoying sometimes, but it is still the best publishing platform out there.
  • Wordpress widgets are weak. They do not allow for the level of customization that i usually like to have.
  • To anyone who has tangled with it, the backend implementation of the tagging / taxonomy system is nightmarish, which is surprising because it is so reasonable in bbPress.
  • Transparent pngs are awesome.
  • I hate it when flexibility is sacrificed for convenience. I am using several plugins that required way too much work in order to use the way i wanted to. For example, the “add this” plugin uses filters instead of simply allowing template tags, so it shows up where it wants to, instead of where i want it to. The twitter tools plugin only provides its front end functionality via a widget, rather than providing some means of using it in any other way. :p
  • IE7 is barely an improvement, it has been an unreal pain in the ass during the creation of this site.
  • I desire to do everything in excessively flamboyant ways that are completely unreasonable, inaccessible, and totally lacking in subtlety.
  • CSS3 needs to happen sooner.
  • so does IE8
  • i love making websites.
addthis
  1. nother's gravatar

    I actually got here by accident, but I must say you have a very pretty site.

Leave a Reply

ok to use:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

bonus!

If you want to post code, you can use:

<pre lang="[language]">[code]</pre>

Where [language] is a valid geshi language type, and where [code] is your code.