Javascript Image Preloader with Image Swapping Function

In this post you will find a simple image swapping function that will work for all images that need a hover effect.

The ‘img’ argument is required of course. I can either be a reference the the image object itself (eg: swapImg(this)) or a string (eg: swapImg(‘ImageID’)) if calling the function from somewhere other than within the <img> tag. If no arguments are passed, it aborts execution. If a string is passed, we first make sure that getElementById is supported. Then we check to make sure the ID passed is valid, of not we return.

After ‘img’ has been checked and confirmed, we move on to the 2 optional arguments. The ‘find’ argument (case-sensitive) indicates the string we are looking for at the end of the image src attribute. If the string is found, we know that the image is currently in the ‘over’ state and we replace ‘find’ with ‘repl’ to return the image back to it’s normal state. On the other hand, if ‘find’ is not located, we rebuild the src attribute and insert ‘repl’ at the end of the image url (before the file extension) to activate the hover state. Continue reading

Disable/Hide Internet Explorer’s Compatibility View Button

If you are an active web developer, you have likely been plagued by Internet Explorer’s Compatibility View button. I have seen so much poor advice about disabling this feature in IE7+. Post after post after post people suggest forcing a specific version for emulation. Haven’t we all learned that allowing IE to dictate how we code a page is a BAD idea?

If you properly code your pages to use standards and a proper document type definition or <!DOCTYPE>, why does IE persist in not doing what it’s told and letting visitors make potentially baddecisions. A user can accidentally click the Compatibility View button or have compatibility mode always enabled by default (OMG!). If you want to make sure IE or the user doesn’t dictate how you code, or how the layout is butchered by compatibility view, add the following to your <head> section…

Continue reading

Validate email domain using PHP’s checkdnsrr with CodeIgniter

I recently noticed a bunch of garbage registrations for an existing client’s newsletter function written quite some time ago. After making a copy of the subscribers table and “cleaning” the data (proper-cased names, lowercased emails, etc), I started looking at ways to limit these automated registrations.

The client would not be happy with registration confirmation emails, so I decided to make a callback function for use with CodeIgniter’s Form Validation library. Since it was only needed in one location, this method made more sense to me than extending the main library.

Here is the function…

Continue reading

GoDaddy, Filezilla and getting blocked while uploading via FTP

I write code for a growing number of clients who host their sites with GoDaddy and Wild West Domains (GoDaddy’s Resller Program). My aresnal includes Photoshop & Expression Web (not a huge fan of Dreamweaver) for visual layout, Filezilla for uploading, and Notepad++ for coding (tried nppFTP plugin for FTP, that didn’t last long, too “buggy”).

Frequently I would be uploading a files after making my initial FTP connection and within a few minutes the connection would time out and I would be unable to reconnect. I figured since it was a shared hosting environment, their hardware and/or software firewall was just being very cautious. After cloning my PC’s IP address in my local router and restarting my cable modem (force renew my IP), I was able to resume work. Inevitably the issue would creep back up quickly.

Continue reading

CodeIgniter, MySQL and User-Defined Variables

The lesson for today is… how to sort records sequentially on a numeric column and provide the ability to move records up and down in the existing sort order.

There are 2 functions I use for manual record sorting to change the display order in admin/user areas. The following example is based on a “pages” table and the commands are located in a model. For this example, the table has the following fields: page_id (int), page_sort (smallint), page_title (varchar), and page_content(text).

Continue reading

XHTML compliant external links based on anchor href attribute

The inspiration for this code was New-Window Links in a Standards-Compliant World. They were using the “rel” attribute and checking it for “external” to update the anchor’s target attribute. I lean towards not opening new windows unless it’s an off-site link to improve the user’s experience. Furthermore, I try to avoid adding more code when I can simply use an element’s existing attributes to serve my needs.

Continue reading

Table/record sorting made easier for CodeIgniter

In a project I am currently working on, I needed a way to efficiently allow sorting of the records in an admin area while preserving the functionality of a search function (that passes the query in the url) and the pagination library. Initially it started off as a section of each method where sorting was allowed (record lists of things like countries, states, etc). My sorting routines finally ended up being put into their own helper.

Continue reading

Less 404 errors by adding robots.txt to your site

Even if you don’t think you need it, it’s still good practice to provide a “robots.txt” file in the root directory of your site for search engine spiders to find. Not only will it remove the 404s from your error_log (happens every time a spider/bot looks for it and it doesn’t exist), but it also provides a quick and efficient way to block certain sections of your site from being indexed. This is by far a better method than adding rel=”nofollow” to your links or the following meta tag to the header of each page in question.

<meta name="robots" content="noindex, nofollow" />

The most basic robots.txt file would include the following. This tells the search engines to index everything it can find.

Continue reading

Rewrite all unresolvable URLs to a script using .htaccess

Another simple one.

The following code checks to see if the request is for a real directory or file and reroutes the request to “/myscript.php” if not.

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ myscript.php?/$1 [L]
</IfModule>

Take note of the the excamation point (it means “not”). To do the opposite (reroute valid directory/file requests through a script) just change the rewrite conditions (RewriteCond) to…

Continue reading

Force “www” subdomain using .htaccess

Short and sweet.

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{HTTP_HOST} !^www\.
  RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
</IfModule>

And if you want to remove the “www” portion, just change the condition (RewriteCond) and rule (RewriteRule)…

You may ask “Why should I do this?” and the asnwers are simple. It promotes linking uniformity and ensures you don’t end up being negatively affected by having “duplicate content” across (sub)domains in search engine indexes.

Codeigniter Image_lib convert() v1.3 JPG to GIF to PNG

I recently performed some code-fixing for a library extension I found over in CodeIgniter’s forum. The mod provided a convert() method for the Image_lib library. It lets you to convert (duh) the original image from/to JPG (default), GIF or PNG and optionally delete the original image.

When I first added this extension into my CI distribution, everything still worked without throwing errors so I started calling the new function. Then…

Continue reading