Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

« Newer Snippets
Older Snippets »
10 total  XML / RSS feed 

Standard CSS helpers

Some really common layout classes I use in almost every CSS file.

/********* helpers *********/
.floatRight { float: right; }
.floatLeft  { float: left; }
.right  { text-align: right; }
.left   { text-align: left; }
.center { text-align: center; }
.clear, .clearer { clear: both; }
.block  { display: block; }

Make your JSP an XHTML

By default, a JSP file won't be considered as an XHTML stream by the web browser. This will make things such as XForms not being rendered since the browser XForms plugins/addons render Xforms only for XHTML pages. To make a JSP file as XHTML, put this at the beginning of the JSP file

<?xml version="1.0" encoding="utf-8"?>
<% response.setContentType("application/xhtml+xml"); %>


Note that the XML declaration has to be in the first line for a valid XHTML. Of course, after doing this, you need to make all your tags valid XML in your JSP as well.

XHTML 1.0 Strict template

// So I can stop typing it. Includes the basic DIVs I aways seem to use.

DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>title>

/html; charset=UTF-8" />
"imagetoolbar" content="no" />
"MSSmartTagsPreventParsing" content="true" />
"stylesheet" type="text/css" href="/styles/main.css" type="text/css" media="screen, projection" />


"stylesheet" type="text/css" href="/styles/print.css" type="text/css" media="print" />


"page">
"header">

Header

"content">
"footer">

Footer

XHTML 1.0 Transitional template

// So I can stop retyping it all the time

DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>title>
        
/html; charset=UTF-8" />
"imagetoolbar" content="no" />
"MSSmartTagsPreventParsing" content="true" />
"stylesheet" type="text/css" href="/styles/main.css" />

"page">
"header">

Header

"content">
"footer">

Footer

XSLT and XHTML DTD's

A quick code snippet to include the right XHTML DTD in your XSL generated output. This took me a bit of reseach to find out:

<xsl:output 
method="xml" 
encoding="utf-8" 
omit-xml-declaration="yes" 
indent="no" 
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" 
doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" />


Another tidbit I found today was:

exclude-result-prefixes="media" 


to exclude unwanted namespace declarations from the output.

XSLT XHTML Template



xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml">

indent="yes"
omit-xml-declaration="yes"
doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" />









test1




















Selecting and checking items from HTML menus

This code allows you to pass in an HTML menu (select, radio, or checkbox) along with a single choice or an array of choices to be selected. The choice(s) should be contained literally in the value attribute.

This is handy for making persistent forms without polluting the HTML with PHP logic. For example, you could initialize all your menus to have nothing selected and then pass in GET or POST variables in case the form was not accepted, easily making form selections persistent.

/**
 * For select menus.
 */
function select_from_menu($menu,$choices,$deselect=true) {
  if ($deselect) $menu = preg_replace('/ selected(="selected")?/','',$menu);
  if(!is_array($choices)) {
    $menu = preg_replace('/(value="'.preg_quote($choices).'")/',"$1 selected=\"selected\"",$menu);
  } else foreach($choices as $value) {
    $menu = preg_replace('/(value="'.preg_quote($value).'")/',"$1 selected=\"selected\"",$menu);
  }
  
  return $menu;
}

/**
 * For radio buttons and checkboxes.
 */
function check_from_menu($menu,$choices,$deselect=true) {
  if ($deselect) $menu = preg_replace('/ checked(="checked")?/','',$menu);
  if(!is_array($choices)) {
    $menu = preg_replace('/(value="'.preg_quote($choices).'")/',"$1 checked=\"checked\"",$menu);
  } else foreach($choices as $value) {
    $menu = preg_replace('/(value="'.preg_quote($value).'")/',"$1 checked=\"checked\"",$menu);
  }

  return $menu;
}

Clear form with Javascript

Purpose: Clear all text fields in a form

Other important features:
- Resulting page must be valid XHTML 1.1
- JS must work in Firefox 1.0 and MS Internet Explorer 6.0
- JS must never generate any errors, even non-fatal ones, in the Firefox Javascript Console
- JS should only clear text input (no hidden fields or visual controls)
- Code must be readable and properly indented

Looking around the web, this was not easy to find. After a few hours of searches, and some back'n'forth at Webmaster World, I got a working version.

In XHTML header:

<script type="text/javascript" src="clear-form-input.js">script>

In html/form/div

<script type="text/javascript">
  // CDATA[
    var el = document.createElement("input");
    el.setAttribute("id", "clearButton");
    el.setAttribute("type", "button");
    el.setAttribute("value", "Clear");
    document.getElementById("deliciousFormDiv").appendChild(el);
    addEvent(el, 'click', function(){ clearForm('delicious');} );
  // ]]>
script>

In clear-form-input.js:

function clearForm(formIdent) 
{ 
  var form, elements, i, elm; 
  form = document.getElementById 
    ? document.getElementById(formIdent) 
    : document.forms[formIdent]; 

        if (document.getElementsByTagName)
        {
                elements = form.getElementsByTagName('input');
                for( i=0, elm; elm=elements.item(i++); )
                {
                        if (elm.getAttribute('type') == "text")
                        {
                                elm.value = '';
                        }
                }
        }

        // Actually looking through more elements here
        // but the result is the same.
        else
        {
                elements = form.elements;
                for( i=0, elm; elm=elements[i++]; )
                {
                        if (elm.type == "text")
                        {
                                elm.value ='';
                        }
                }
        }
}
        
function addEvent(elm, strEvent, fnHandler)
{
        return ( elm.addEventListener
        ? elm.addEventListener( strEvent, fnHandler, false)
        : elm.attachEvent( 'on'+strEvent, fnHandler)
        );
}

mod_rewrite rules to serve application/xhtml+xml

The following mod_rewrite rules will serve HTML files as application/xhtml+xml to supporting browsers.

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_ACCEPT} application/xhtml\+xml
RewriteCond %{HTTP_ACCEPT} !application/xhtml\+xml\s*;\s*q=0
RewriteCond %{REQUEST_URI} \.html$
RewriteCond %{THE_REQUEST} HTTP/1\.1
RewriteRule .* - [T=application/xhtml+xml]


Source: Mark Pilgrim, The Road to XHTML 2.0: MIME Types.

Serve XHTML 1.1 Properly

Place this in a .htaccess file or your Apache configuration. Note that this will redirect noncompliant users to a chosen URL. All XHTML 1.1 pages must be saved with the .xhtml extension.

AddType application/xhtml+xml .xhtml

<IfModule mod_security.c>
    SecFilterEngine On
    SecFilterScanOutput On
    SecFilterOutputMimeTypes "application/xhtml+xml"
    SecFilterDefaultAction "pass"
    SecFilterSelective "HTTP_ACCEPT" "!application/xhtml\+xml" "redirect:http://www.webstandards.org/upgrade/"
IfModule>
« Newer Snippets
Older Snippets »
10 total  XML / RSS feed