Posts Tagged ‘Programming’

IE and DOM buttons - The correct way

Tuesday, August 26th, 2008

I have had so many problems creating buttons in IE using the dom that I had gone to just using images as buttons and adding them. Turns out there is one simple thing you can do to get it to work. Create the button, then add it to the page…

Here is what I was doing:

var button = item.appendChild(document.createElement('input'));
button.type='button';
button.value='THIS IS A BUTTON';

This code always threw an error, however if you do it this way (creating the button, and then adding it) it works fine:

var button = document.createElement('input');
button.type='button';
button.value='THIS IS A BUTTON';
button = item.appendChild(button);

Have fun dynamically creating forms that will work in most browsers!

Remove yourself from your google analytics

Thursday, March 6th, 2008

Are your Google Analytics stats messed up because you go to your site all the time? Would you like to fix that? Well Google provides a way you could block the traffic from a certain IP address. But if you are checking it from a range of computers then that doesn’t really do you any good. You can block a range but what if you use your laptop all over town and connect into different wireless access points. Or in my case when I go to school I get an new IP every day if not more. So what I decided to do was to defeat it with cookies.
The first step is to create a page that will store a cookie to the computer. For this site I added it into the submission page for these posts. No one else comes here so I shouldn’t have to worry about people accidentally getting the cookie set on their own computer. (A side benefit of having it on a page that I use a bunch is that if I clear my cookies it comes back without me having to think about it.) To create a cookie on a page using php you put this at the top:

<?PHP
setcookie (”analytics”, “no_analytics”);
?>

This sets a cookie with the name analytics and the value no_analytics set to that cookie.
Next, we put an if statement around where we record the analytics (for me this is in the header so it is called on every page.):

<?

if($_COOKIE["analytics"] != ‘no_analytics’) // if there is a cookie for this site that contains this information then the analytics will not be counted
{

?>

This If statement will check the cookie and if it exists it will not print out the javascript that sends the information to the Google servers. As long as you have the cookie set in the browsers you use, you will be able to visit the site without being counted. And if you visit from somewhere else - if you want you can hit the page, store the cookie and delete it when you are finished!