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

About this user

Alan Coleman www.alancoleman.co.uk

« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS 

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"

function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
     anchor.target = "_blank";
    }

}
}
window.onload = externalLinks;

//////////////

<a href="whatever" rel="external" title="whatever">Whatever</a>

Form name not valid in strict XHTML

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

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

<form id="search"...

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

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.

    	<input type="submit" value="Search" onclick="javascript:return text_validate()" />
   	<input type="reset" value="Reset" />
    	<input type="button" value="Show All News" onclick="javascript:return showAll();" title="Show all news button." />

Could be shown as:

        <a class="blockbutton" title="Search" href="javascript:document.search_form.submit();" onclick="javascript:return text_validate();">Search</a>	
	<a class="blockbutton" title="Reset" href="javascript:document.search_form.reset();">Reset</a>
	<a class="blockbutton" title="Show All News" href="javascript:showAll();">Show All News</a>



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

<script type="text/javascript">
  function doSubmit(){    
        document.getElementById("whatever").submit();
        return true;   
     }   
</script>

<a href="#" onclick="doSubmit();return false;" title="Search">Link</a>

Conditional comment

From the microsoft website. Better than browser sniffing, the content within the tags will be shown if the condition within [] is met, in the this case IE, but you could also use IE 7 for example
  <!--[if IE]>
    Content
  <![endif]-->

  <!--[if lt IE6]> //lower than IE6
    Content
  <![endif]-->


and the XSL version

  <xsl:comment>[if IE 7]&gt;
    Content &lt;![endif]</xsl:comment>


When producing XHTML output with XSL files, you have to use

The method="xml" is needed if you want XHTML valid output, "xhtml" is not a valid parameter.

The encoding="us-ascii" is needed if you want pound, euro, nbsp and any other html entities to show up correctly.

<xsl:output method="xml" omit-xml-declaration="yes" encoding="us-ascii"/>
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS