My Blog

10 Useful code snippets for web developers

Posted on: August 9, 2009

In this post I want to suggest you 10 useful code snippets for web developers based on some frequetly asked questions I received in the past months for my readers. This collection include several CSS, PHP, HTML, Ajax and jQuery snippets. Take a look!

1. CSS Print Framework
Hartija is an universal Cascading Style Sheets Framework for web printing. To use this framework download the CSS file here and use this line of code into your web pages:

2. CSS @font-face
This snippet allows authors to specify online custom fonts to display text on their webpages without using images:

@font-face {
font-family: MyFontFamily;
src: url(‘http://’);
}

3. HTML 5 CSS Reset
Richard Clark made a few adjustments to the original CSS reset released from Eric Meyers.

4. Unit PNG Fix
This snippet fixes the png 24 bit transparency with Internet Explorer 6.

5. Tab Bar with rounded corners
This code illustrates how to implement a simple tab bar with rounded corners:

6. PHP: Use isset() instead of strlen()
This snippet uses isset() instead strlen() to verify a PHP variable (in this example $username) is set and is at least six characters long. (via Smashing Magazine).

7. PHP: Convert strings into clickable url
This snippet is very useful to convert a string in a clickable link. I used this snippet for several tutorials; for example take a look at this link Simple PHP Twitter Search ready to use in your web projects where I used this snippet to convet into a clickable link all textual links contained in a tweet.

<? php
function convertToURL($text) {
$text = preg_replace("/([a-zA-Z]+:\/\/[a-z0-9\_\.\-]+"."[a-z]{2,6}[a-zA-Z0-9\/\*\-\_\?\&\%\=\,\+\.]+)/"," $1“, $text);
$text = preg_replace(“/[^a-z]+[^:\/\/](www\.”.”[^\.]+[\w][\.|\/][a-zA-Z0-9\/\*\-\_\?\&\%\=\,\+\.]+)/”,” $1“, $text);
$text = preg_replace(“/([\s|\,\>])([a-zA-Z][a-zA-Z0-9\_\.\-]*[a-z” . “A-Z]*\@[a-zA-Z][a-zA-Z0-9\_\.\-]*[a-zA-Z]{2,6})” . “([A-Za-z0-9\!\?\@\#\$\%\^\&\*\(\)\_\-\=\+]*)” . “([\s|\.|\,\<])/i", "$1$2$4″,
$text);
return $text;
}
?>

8. jQuery: Ajax call
This is the most simple way to implement an Ajax call using jQuery. Change formId and inputFieldId with the ID of the form and input field you have to submit:

$(document).ready(function(){
$(“form#formId”).submit(function() {
inputField = $(‘#inputFieldId’).attr(‘value’);
$.ajax({
type: “POST”,
url: “yourpage.php”,
cache: false,
data: “inputField =”+ inputField,
success: function(html){
$(“#ajax-results”).html(html);
}
});
return false;
});
});

9. CSS Layouts Collections
This page contains a collection of over 300 grids and CSS layout ready to use in your projects. Take a look, it’s very useful.

10. Simple versatile multilevel navigation Menu
Several months ago I found this simple code (HTML + CSS + JavaScript for jQuery) to implement a versatile multilevel navigation menu (please send me the original source if you find it!). I think it’s the simpler and faster way to do that. The result is something like this:

The only thing you have to do is to create nested &ltul> lists into a main list with id=”nav”, in this way:

  • Link 1
  • Link 2
  • Link 3
    • Link 3.1
    • Link 3.2

      …and use this basic CSS code (you have to modify it to customize the layout of this menu with the style of your site!):

      #nav, #nav ul{
      margin:0;
      padding:0;
      list-style-type:none;
      list-style-position:outside;
      position:relative;
      line-height:26px;
      }
      #nav a:link,
      #nav a:active,
      #nav a:visited{
      display:block;
      color:#FFF;
      text-decoration:none;
      background:#444;
      height:26px;
      line-height:26px;
      padding:0 6px;
      margin-right:1px;
      }
      #nav a:hover{
      background:#0066FF;
      color:#FFF;
      }
      #nav li{
      float:left;
      position:relative;
      }
      #nav ul {
      position:absolute;
      width:12em;
      top:26px;
      display:none;
      }
      #nav li ul a{
      width:12em;
      float:left;
      }
      #nav ul ul{
      width:12em;
      top:auto;
      }
      #nav li ul ul {margin:0 0 0 13em;}
      #nav li:hover ul ul,
      #nav li:hover ul ul ul,
      #nav li:hover ul ul ul ul{display:none;}
      #nav li:hover ul,
      #nav li li:hover ul,
      #nav li li li:hover ul,
      #nav li li li li:hover ul{display:block;}

      …and this is the JavaScript code for jQuery you have to copy in the tag of the pages that use this menu:

      function showmenu(){
      $(“#nav li”).hover(function(){
      $(this).find(‘ul:first’).css({visibility:”visible”, display:”none”}).show();
      }, function(){
      $(this).find(‘ul:first’).css({visibility:”hidden”});
      });
      }

      $(document).ready(function(){
      showmenu();
      });

      Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Pages

Web Dev Tutorials

%d bloggers like this: