Never been to DZone Snippets 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

« Newer Snippets
Older Snippets »
Showing 11-20 of 31 total

Running Haml stand-alone

This Ruby code converts HAML to XHTML. code based on the example given at 'A HAML Server for Web Designers' http://urltea.com/23j8 [wiseheartdesign.com].

   1  
   2  require 'rubygems'
   3  require 'haml'
   4  
   5  def parse_haml(string)
   6    engine = Haml::Engine.new(string)
   7    engine.render
   8  end
   9  
  10  parse_haml("#hello")


Note: The above code worked on Ubuntu 7.10, and on Gentoo I declared require 'haml/engine' as an alternative to requiring 'rubygems' and 'haml'.

Safari 3 - Resizable Text Fields

With Safari 3, you can resize the TEXTAREA, but you can control this properties with CSS3.

   1  
   2  
   3  <textarea cols="30" rows="10" style="resize: both;">www.AB-D.fr presents the new TEXTAREA</textarea>
   4  
   5  <textarea cols="30" rows="10" style="resize: horizontal;">www.AB-D.fr presents the new TEXTAREA</textarea>
   6  
   7  <textarea cols="30" rows="10" style="resize: vertical;">www.AB-D.fr presents the new TEXTAREA</textarea>
   8  
   9  <textarea cols="30" rows="10" style="resize: none;">www.AB-D.fr presents the new TEXTAREA</textarea>
  10  


Links:
http://www.apple.com/safari/
http://www.w3.org/TR/css3-ui/#resize

IE doesn't support element.setAttritube('style')

   1  
   2  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
   3  <html xmlns="http://www.w3.org/1999/xhtml" version="-//W3C//DTD XHTML 1.1//EN" xml:lang="fr">
   4  <head>
   5  
   6  <title>.setAttribute('style','');</title>
   7  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   8  
   9  <script type="text/javascript">
  10  <!--
  11  
  12  window.onload = function() {
  13  	if (navigator.appName == 'Microsoft Internet Explorer') {
  14  		document.getElementById('test').style.cssText = 'background:gray; color:white;';
  15  	} else {
  16  		/* document.getElementById('test').style.cssText = 'background:gray; color:white;'; */
  17  		document.getElementById('test').setAttribute('style', 'background:gray; color:white;');
  18  	}
  19  }
  20  
  21  -->
  22  </script>
  23  
  24  </head>
  25  
  26  <body>
  27  
  28  <div id="test">document.getElementById('test').setAttribute('style', 'background:gray; color:white;')</div>
  29  
  30  </body>
  31  
  32  </html>

Another way of written "target"

Another way of written "target" and valid in XHTML

   1  
   2  
   3  <a href="http://www.google.com/" onclick="window.open(this.href); return false;">Link</a>
   4  

target= not valid in XHTML

Used for pages with strict doctypes (i.e. no target="_blank"). Automagically adds them back in to links with rel="external"

   1  
   2  function externalLinks() {
   3   if (!document.getElementsByTagName) return;
   4   var anchors = document.getElementsByTagName("a");
   5   for (var i=0; i<anchors.length; i++) {
   6     var anchor = anchors[i];
   7     if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
   8       anchor.target = "_blank";
   9      }
  10  
  11  }
  12  }
  13  window.onload = externalLinks;
  14  
  15  //////////////
  16  
  17  <a href="whatever" rel="external" title="whatever">Whatever</a>
  18  

Form name not valid in strict XHTML

<form name="search" id="search"...

'name' is not valid in strict XHTML. So instead use the id

   1  
   2  <form id="search"...
   3  
   4  <a href="#" onclick="document.forms['search'].submit();return false" title="Search">Search</a>

Object alternative to iframe for XHTML 1.0 strict

   1  
   2  <object data="mypage.php" type="text/html"></object>

RSS/Atom Autodiscovery

// description of your code here

   1  
   2  <link rel="alternate" type="application/rss+xml" title="RSS Feed" href="rss.xml" />
   3  
   4  <!-- or... //-->
   5  
   6  <link rel="alternate" type="application/atom+xml" title="Atom Feed" href="Atom.xml" />
   7  

Simple w3c Validator

// This function will return either a true or false (boolean) value, whether a certain site contains valid code based on w3 standards. Requires the Snoopy PHP class.
// Could be used in many situations where a simple "is valid" or "is not valid" result is required.

   1  
   2  <?php
   3  function w3Valid($url) {
   4  	require("Snoopy.class.php");
   5  	$snoopy = new Snoopy;
   6  
   7  	$snoopy->fetchtext("http://validator.w3.org/check?uri=" . $url . "&output=soap12");
   8  	$validator_output = $snoopy->results;
   9  
  10  	if(preg_match("/false/", $validator_output)) {
  11  		return false;
  12  	} else {
  13  		return true;
  14  	}
  15  }
  16  
  17  ?>

Javascript commands from 'a' tags rather than buttons

In this instance the relevant form is called 'search_form'. This is specified in the a tag version because it's not an input and as such cannot have submit or reset associated with it.

   1  
   2      	<input type="submit" value="Search" onclick="javascript:return text_validate()" />
   3     	<input type="reset" value="Reset" />
   4      	<input type="button" value="Show All News" onclick="javascript:return showAll();" title="Show all news button." />
   5  
   6  Could be shown as:
   7  
   8          <a class="blockbutton" title="Search" href="javascript:document.search_form.submit();" onclick="javascript:return text_validate();">Search</a>	
   9  	<a class="blockbutton" title="Reset" href="javascript:document.search_form.reset();">Reset</a>
  10  	<a class="blockbutton" title="Show All News" href="javascript:showAll();">Show All News</a>
  11  


Okay, as pointed out by Blonkm this is bad for usability. A better way of doing things would be like this:

   1  
   2  <script type="text/javascript">
   3    function doSubmit(){    
   4          document.getElementById("whatever").submit();
   5          return true;   
   6       }   
   7  </script>
   8  
   9  <a href="#" onclick="doSubmit();return false;" title="Search">Link</a>
  10  
« Newer Snippets
Older Snippets »
Showing 11-20 of 31 total