<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Frederik Vig - ASP.NET developer &#187; JavaScript</title>
	<atom:link href="http://www.frederikvig.com/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.frederikvig.com</link>
	<description></description>
	<lastBuildDate>Wed, 28 Jul 2010 14:08:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>CSS and JavaScript compression and bundling</title>
		<link>http://www.frederikvig.com/2010/06/css-and-javascript-compression-and-bundling/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=css-and-javascript-compression-and-bundling</link>
		<comments>http://www.frederikvig.com/2010/06/css-and-javascript-compression-and-bundling/#comments</comments>
		<pubDate>Sat, 19 Jun 2010 22:26:50 +0000</pubDate>
		<dc:creator>Frederik Vig</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://www.frederikvig.com/?p=1298</guid>
		<description><![CDATA[In the last post in Create an EPiServer site from scratch I go through some optimization tips and tricks from YSlow. One of these are CSS and JavaScript compression and bundling of files to create fewer HTTP requests and to send the least amount of data back to the client as possible, removing whitespaces, comments [...]]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://www.frederikvig.com/2009/12/part-8-preparing-for-launch-create-an-episerver-site-from-scratch/">last post</a> in <a href="http://www.frederikvig.com/2009/12/introduction-create-an-episerver-site-from-scratch/">Create an EPiServer site from scratch</a> I go through some optimization tips and tricks from <a href="http://developer.yahoo.com/yslow/">YSlow</a>. One of these are CSS and JavaScript compression and bundling of files to create fewer HTTP requests and to send the least amount of data back to the client as possible, removing whitespaces, comments etc, that we don&#8217;t need to run our code. </p>
<p>In the past I&#8217;ve done this as part of my deployment process, but recently I&#8217;ve come across a tool called <a href="http://www.codethinked.com/post/2010/05/26/SquishIt-The-Friendly-ASPNET-JavaScript-and-CSS-Squisher.aspx">SquishIt</a>. Which basically is a wrapper for <a href="http://developer.yahoo.com/yui/compressor/">YUI Compressor</a> (you can easily replace it with your compressor of choice). It is dead simple to use, you simply add a reference to SquishIt.Framework.dll in your project, and replace your old CSS and JavaScript links and script tags with code like this.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;%=</span> Bundle.<span style="color: #0000FF;">Css</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
          .<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;~/Styles/screen.css&quot;</span><span style="color: #000000;">&#41;</span>
          .<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;~/Styles/print.css&quot;</span><span style="color: #000000;">&#41;</span>
          .<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;~/Styles/mobile.css&quot;</span><span style="color: #000000;">&#41;</span>
          .<span style="color: #0000FF;">Render</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;~/Styles/combined-#.css&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">%&gt;</span>
&nbsp;
<span style="color: #008000;">&lt;%=</span> Bundle.<span style="color: #0000FF;">JavaScript</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
          .<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;~/Scripts/belatedPNG-0.0.8a-min.js&quot;</span><span style="color: #000000;">&#41;</span>
          .<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;~/Scripts/ie6.js&quot;</span><span style="color: #000000;">&#41;</span>
          .<span style="color: #0000FF;">Render</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;~/Scripts/ie6combined-#.js&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">%&gt;</span></pre></div></div>

<p>Using a fluent interface makes it easy to add more file references, ending with the Render method that takes a parameter for where to store the combined and minimized file. </p>
<p>It is very easy to toggle SquishIt on and off. Simply set debug=&#8221;true&#8221; in your web.config file to turn it off and render CSS and JavaScript files as normal, great for when you need to debug your code. Set debug=&#8221;false&#8221; to enable compression and bundling again.</p>
<p>Best part is that it only took me 2 minutes to setup and add to my current project! Works like a charm so far <img src='http://www.frederikvig.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .<strong>Related Posts:</strong>
<ul class="similar-posts">
<li><a href="http://www.frederikvig.com/2009/08/adding-css-and-javascript-files-dynamically-to-your-asp-net-page/" rel="bookmark" title="August 15, 2009">Adding CSS and JavaScript files dynamically to your ASP.NET page</a></li>
<li><a href="http://www.frederikvig.com/2009/12/part-8-preparing-for-launch-create-an-episerver-site-from-scratch/" rel="bookmark" title="December 24, 2009">Part 8: Preparing for launch – Create an EPiServer site from scratch</a></li>
<li><a href="http://www.frederikvig.com/2009/07/episerver-web-controls-newslist/" rel="bookmark" title="July 25, 2009">EPiServer web controls: NewsList</a></li>
<li><a href="http://www.frederikvig.com/2009/07/episerver-web-controls-pagelist/" rel="bookmark" title="July 20, 2009">EPiServer web controls: PageList</a></li>
<li><a href="http://www.frederikvig.com/2010/01/episerver-code-walkthrough-1-404-handler/" rel="bookmark" title="January 29, 2010">EPiServer code walkthrough #1 &#8211; 404 handler</a></li>
</ul>
<p><!-- Similar Posts took 35.386 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.frederikvig.com/2010/06/css-and-javascript-compression-and-bundling/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating a contact form with ASP.NET MVC</title>
		<link>http://www.frederikvig.com/2010/05/creating-a-contact-form-with-asp-net-mvc/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=creating-a-contact-form-with-asp-net-mvc</link>
		<comments>http://www.frederikvig.com/2010/05/creating-a-contact-form-with-asp-net-mvc/#comments</comments>
		<pubDate>Thu, 13 May 2010 15:37:07 +0000</pubDate>
		<dc:creator>Frederik Vig</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>

		<guid isPermaLink="false">http://www.frederikvig.com/?p=1239</guid>
		<description><![CDATA[We&#8217;re going to create a contact form in ASP.NET MVC 2.0, that uses Ajax to send the form data and that uses client side validation to improve the user experience for our users. Start by creating a new ASP.NET MVC 2.0 web application project in Visual Studio. This will create a new project with the [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;re going to create a contact form in ASP.NET MVC 2.0, that uses Ajax to send the form data and that uses client side validation to improve the user experience for our users.</p>
<p>Start by creating a new ASP.NET MVC 2.0 web application project in Visual Studio.</p>
<p>
<img src="http://www.frederikvig.com/wp-content/uploads/contact-form/create-new-aspnet-mvc-application.png" alt="New project dialog in Visual Studio" />
</p>
<p>This will create a new project with the default ASP.NET MVC 2.0 sample code. </p>
<p>Inside the Models folder create a new class, and give it the name: Contact.cs. The class is very simple with just some properties for Name, Email and Comment.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System.ComponentModel</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.ComponentModel.DataAnnotations</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> ContactForm.<span style="color: #0000FF;">Models</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Contact
    <span style="color: #000000;">&#123;</span>
        <span style="color: #000000;">&#91;</span>Required<span style="color: #000000;">&#40;</span>ErrorMessage <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;You need to fill in a name&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">&#91;</span>DisplayName<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Name&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Name <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>Required<span style="color: #000000;">&#40;</span>ErrorMessage <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;You need to fill in an email address&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">&#91;</span>DataType<span style="color: #000000;">&#40;</span>DataType.<span style="color: #0000FF;">EmailAddress</span>, ErrorMessage <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Your email address contains some errors&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">&#91;</span>DisplayName<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Email address&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Email <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>Required<span style="color: #000000;">&#40;</span>ErrorMessage <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;You need to fill in a comment&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">&#91;</span>DisplayName<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Your comment&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Comment <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Notice the Required, DisplayName and DataType attributes. This is called DataAnnotations, and is a new feature in ASP.NET MVC 2.0, which helps us add validation logic to our models.</p>
<p>Next step is creating the /home/contact URL and the contact view. Open up HomeController.cs, and add the following method.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> ActionResult Contact<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">return</span> View<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Place your cursor inside View() and press Ctrl+M, Ctrl+V, or just right-click and choose Add View. Check the &#8220;Create a strongly-typed view&#8221; and type in: ContactForm.Models.Contact.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;%</span>@ Page Title<span style="color: #008000;">=</span><span style="color: #666666;">&quot;&quot;</span> Language<span style="color: #008000;">=</span><span style="color: #666666;">&quot;C#&quot;</span> MasterPageFile<span style="color: #008000;">=</span><span style="color: #666666;">&quot;~/Views/Shared/Site.Master&quot;</span> Inherits<span style="color: #008000;">=</span><span style="color: #666666;">&quot;System.Web.Mvc.ViewPage&lt;ContactForm.Models.Contact&gt;&quot;</span> <span style="color: #008000;">%&gt;</span>
&nbsp;
<span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>Content ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Content1&quot;</span> ContentPlaceHolderID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;TitleContent&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span><span style="color: #008000;">&gt;</span>
	Contact
<span style="color: #008000;">&lt;/</span>asp<span style="color: #008000;">:</span>Content<span style="color: #008000;">&gt;</span>
&nbsp;
<span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>Content ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Content2&quot;</span> ContentPlaceHolderID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;MainContent&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span><span style="color: #008000;">&gt;</span>
&nbsp;
    <span style="color: #008000;">&lt;</span>h2<span style="color: #008000;">&gt;</span>Contact<span style="color: #008000;">&lt;/</span>h2<span style="color: #008000;">&gt;</span>
&nbsp;
<span style="color: #008000;">&lt;/</span>asp<span style="color: #008000;">:</span>Content<span style="color: #008000;">&gt;</span></pre></div></div>

<p>We can now create the form markup.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;%</span>@ Page Title<span style="color: #008000;">=</span><span style="color: #666666;">&quot;&quot;</span> Language<span style="color: #008000;">=</span><span style="color: #666666;">&quot;C#&quot;</span> MasterPageFile<span style="color: #008000;">=</span><span style="color: #666666;">&quot;~/Views/Shared/Site.Master&quot;</span> Inherits<span style="color: #008000;">=</span><span style="color: #666666;">&quot;System.Web.Mvc.ViewPage&lt;ContactForm.Models.Contact&gt;&quot;</span> <span style="color: #008000;">%&gt;</span>
&nbsp;
<span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>Content ContentPlaceHolderID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;TitleContent&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span><span style="color: #008000;">&gt;</span>
	Contact us
<span style="color: #008000;">&lt;/</span>asp<span style="color: #008000;">:</span>Content<span style="color: #008000;">&gt;</span>
&nbsp;
<span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>Content ContentPlaceHolderID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;MainContent&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span><span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;%</span> Html.<span style="color: #0000FF;">EnableClientValidation</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">%&gt;</span> 
    <span style="color: #008000;">&lt;%</span> <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>Ajax.<span style="color: #0000FF;">BeginForm</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> AjaxOptions <span style="color: #000000;">&#123;</span> HttpMethod <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Post&quot;</span>, OnComplete <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Contact.onComplete&quot;</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span> <span style="color: #008000;">%&gt;</span>
        <span style="color: #008000;">&lt;%:</span> Html.<span style="color: #0000FF;">ValidationSummary</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span>, <span style="color: #666666;">&quot;A few fields are still empty&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">%&gt;</span>
        <span style="color: #008000;">&lt;</span>fieldset<span style="color: #008000;">&gt;</span>
            <span style="color: #008000;">&lt;</span>legend<span style="color: #008000;">&gt;</span>Contact us<span style="color: #008000;">&lt;/</span>legend<span style="color: #008000;">&gt;</span>
            <span style="color: #008000;">&lt;</span>div <span style="color: #FF0000;">class</span><span style="color: #008000;">=</span><span style="color: #666666;">&quot;editor-label&quot;</span><span style="color: #008000;">&gt;</span>
                <span style="color: #008000;">&lt;%:</span> Html.<span style="color: #0000FF;">LabelFor</span><span style="color: #000000;">&#40;</span>m <span style="color: #008000;">=&gt;</span> m.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">%&gt;</span>
            <span style="color: #008000;">&lt;/</span>div<span style="color: #008000;">&gt;</span>
            <span style="color: #008000;">&lt;</span>div <span style="color: #FF0000;">class</span><span style="color: #008000;">=</span><span style="color: #666666;">&quot;editor-field&quot;</span><span style="color: #008000;">&gt;&lt;%:</span> Html.<span style="color: #0000FF;">TextBoxFor</span><span style="color: #000000;">&#40;</span>m <span style="color: #008000;">=&gt;</span> m.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">%&gt;</span>
                <span style="color: #008000;">&lt;%:</span> Html.<span style="color: #0000FF;">ValidationMessageFor</span><span style="color: #000000;">&#40;</span>m <span style="color: #008000;">=&gt;</span> m.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">%&gt;</span>
            <span style="color: #008000;">&lt;/</span>div<span style="color: #008000;">&gt;</span>
            <span style="color: #008000;">&lt;</span>div <span style="color: #FF0000;">class</span><span style="color: #008000;">=</span><span style="color: #666666;">&quot;editor-label&quot;</span><span style="color: #008000;">&gt;</span>
                <span style="color: #008000;">&lt;%:</span> Html.<span style="color: #0000FF;">LabelFor</span><span style="color: #000000;">&#40;</span>m <span style="color: #008000;">=&gt;</span> m.<span style="color: #0000FF;">Email</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">%&gt;</span>
            <span style="color: #008000;">&lt;/</span>div<span style="color: #008000;">&gt;</span>
            <span style="color: #008000;">&lt;</span>div <span style="color: #FF0000;">class</span><span style="color: #008000;">=</span><span style="color: #666666;">&quot;editor-field&quot;</span><span style="color: #008000;">&gt;</span>
                <span style="color: #008000;">&lt;%:</span> Html.<span style="color: #0000FF;">TextBoxFor</span><span style="color: #000000;">&#40;</span>m <span style="color: #008000;">=&gt;</span> m.<span style="color: #0000FF;">Email</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">%&gt;</span>
                <span style="color: #008000;">&lt;%:</span> Html.<span style="color: #0000FF;">ValidationMessageFor</span><span style="color: #000000;">&#40;</span>m <span style="color: #008000;">=&gt;</span> m.<span style="color: #0000FF;">Email</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">%&gt;</span>
            <span style="color: #008000;">&lt;/</span>div<span style="color: #008000;">&gt;</span>
            <span style="color: #008000;">&lt;</span>div <span style="color: #FF0000;">class</span><span style="color: #008000;">=</span><span style="color: #666666;">&quot;editor-label&quot;</span><span style="color: #008000;">&gt;</span>
                <span style="color: #008000;">&lt;%:</span> Html.<span style="color: #0000FF;">LabelFor</span><span style="color: #000000;">&#40;</span>m <span style="color: #008000;">=&gt;</span> m.<span style="color: #0000FF;">Comment</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">%&gt;</span>
            <span style="color: #008000;">&lt;/</span>div<span style="color: #008000;">&gt;</span>
            <span style="color: #008000;">&lt;</span>div <span style="color: #FF0000;">class</span><span style="color: #008000;">=</span><span style="color: #666666;">&quot;editor-field&quot;</span><span style="color: #008000;">&gt;</span>
                <span style="color: #008000;">&lt;%:</span> Html.<span style="color: #0000FF;">TextAreaFor</span><span style="color: #000000;">&#40;</span>m <span style="color: #008000;">=&gt;</span> m.<span style="color: #0000FF;">Comment</span>, <span style="color: #FF0000;">10</span>, <span style="color: #FF0000;">25</span>, <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">%&gt;</span>
                <span style="color: #008000;">&lt;%:</span> Html.<span style="color: #0000FF;">ValidationMessageFor</span><span style="color: #000000;">&#40;</span>m <span style="color: #008000;">=&gt;</span> m.<span style="color: #0000FF;">Comment</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">%&gt;</span>
            <span style="color: #008000;">&lt;/</span>div<span style="color: #008000;">&gt;</span>
            <span style="color: #008000;">&lt;</span>p<span style="color: #008000;">&gt;</span>
                <span style="color: #008000;">&lt;</span>input type<span style="color: #008000;">=</span><span style="color: #666666;">&quot;submit&quot;</span> value<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Submit&quot;</span> <span style="color: #008000;">/&gt;</span>
            <span style="color: #008000;">&lt;/</span>p<span style="color: #008000;">&gt;</span>
        <span style="color: #008000;">&lt;/</span>fieldset<span style="color: #008000;">&gt;</span>
        <span style="color: #008000;">&lt;</span>p id<span style="color: #008000;">=</span><span style="color: #666666;">&quot;result&quot;</span><span style="color: #008000;">&gt;&lt;%:</span> TempData<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;Message&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">%&gt;&lt;/</span>p<span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;%</span> <span style="color: #000000;">&#125;</span> <span style="color: #008000;">%&gt;</span>
<span style="color: #008000;">&lt;/</span>asp<span style="color: #008000;">:</span>Content<span style="color: #008000;">&gt;</span></pre></div></div>

<p>&lt;%: &#8230; %&gt; is just a shortcut for HTML encoding the data, a new feature in ASP.NET 4.0. &lt;% Html.EnableClientValidation(); %&gt; enables client side validation for us, and must be included right before the start of the form. Ajax.BeginForm is a helper method that generates the form tag for us and attaches a little JavaScript code for sending the form data with Ajax, if the client supports it, otherwise the form data will be sent like normal.</p>
<p>Lets create the JavaScript method that gets called when the form is finished. Create a new JavaScript file inside the Scripts folder and give it the name: Site.js. Add the Contact object with the property function onComplete.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> Contact <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    onComplete<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>content<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> result <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">eval</span><span style="color: #009900;">&#40;</span>content.<span style="color: #660066;">get_response</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">get_object</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> textNode <span style="color: #339933;">=</span> document.<span style="color: #660066;">createTextNode</span><span style="color: #009900;">&#40;</span>result.<span style="color: #660066;">message</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;result&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>textNode<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I take the JSON result from the server and add the message to the result paragraph.</p>
<p>If you press F5, the site should open up in your default browser. If you add Home/Contact behind the URL, you should be taken to the Contact Us page.</p>
<p>
<img src="http://www.frederikvig.com/wp-content/uploads/contact-form/contact-form.png" alt="Contact us form" />
</p>
<p>For the client side validation to work we need to reference a few JavaScript files. Open up Site.master (located in the shared folder), and add the following code right before the closing body tag.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;">...
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;../../Scripts/MicrosoftAjax.js&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;../../Scripts/MicrosoftMvcAjax.js&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;../../Scripts/MicrosoftMvcValidation.js&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;../../Scripts/Site.js&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p>You should now receive some friendly error messages when you click the submit button without filling out the form properly.</p>
<p>
<img src="http://www.frederikvig.com/wp-content/uploads/contact-form/contact-form-validation.png" alt="Contact us form validation" />
</p>
<p>Next step is adding the action method to HomeController.cs that will handle the form data.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>HttpPost<span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> ActionResult Contact<span style="color: #000000;">&#40;</span>Contact model<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
        <span style="color: #FF0000;">string</span> message <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;There are a few errors&quot;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>ModelState.<span style="color: #0000FF;">IsValid</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
                message <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Thanks! We'll get back to you soon.&quot;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>Request.<span style="color: #0000FF;">IsAjaxRequest</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> JsonResult <span style="color: #000000;">&#123;</span> ContentEncoding <span style="color: #008000;">=</span> Encoding.<span style="color: #0000FF;">UTF8</span>, Data <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #000000;">&#123;</span> success <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</span>, message <span style="color: #008000;">=</span> message <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
        TempData<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;Message&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> message<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">return</span> View<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Notice that I check if the request is an Ajax request, if it is I return a JSON object with the result, otherwise I return the whole view with the message.</p>
<p>You would also add some other logic here for saving the message in some way.</p>
<p>
<img src="http://www.frederikvig.com/wp-content/uploads/contact-form/contact-form-complete.png" alt="The finished form" />
</p>
<p>That&#8217;s it!<strong>Related Posts:</strong>
<ul class="similar-posts">
<li><a href="http://www.frederikvig.com/2009/05/aspnet-validation-and-jquery/" rel="bookmark" title="May 21, 2009">ASP.NET Validation and jQuery</a></li>
<li><a href="http://www.frederikvig.com/2009/10/sending-confirmation-email-to-the-user-when-using-episerver-xforms/" rel="bookmark" title="October 17, 2009">Sending confirmation email to the user when using EPiServer XForms</a></li>
<li><a href="http://www.frederikvig.com/2009/07/asp-net-web-forms-and-jquery-thickbox-plugin/" rel="bookmark" title="July 13, 2009">ASP.NET web forms and jQuery Thickbox plugin</a></li>
<li><a href="http://www.frederikvig.com/2009/07/episerver-web-controls-property/" rel="bookmark" title="July 5, 2009">EPiServer web controls: Property</a></li>
<li><a href="http://www.frederikvig.com/2009/12/part-6-creating-the-xform-page-create-an-episerver-site-from-scratch/" rel="bookmark" title="December 19, 2009">Part 6: Creating the XForm page – Create an EPiServer site from scratch</a></li>
</ul>
<p><!-- Similar Posts took 59.834 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.frederikvig.com/2010/05/creating-a-contact-form-with-asp-net-mvc/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Backup plan when loading the jQuery library from CDN</title>
		<link>http://www.frederikvig.com/2010/04/backup-plan-when-loading-the-jquery-library-from-cdn/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=backup-plan-when-loading-the-jquery-library-from-cdn</link>
		<comments>http://www.frederikvig.com/2010/04/backup-plan-when-loading-the-jquery-library-from-cdn/#comments</comments>
		<pubDate>Tue, 20 Apr 2010 18:30:12 +0000</pubDate>
		<dc:creator>Frederik Vig</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.frederikvig.com/?p=1228</guid>
		<description><![CDATA[In most of my project I load the jQuery library from a CDN, either Google or Microsoft. This ensures that my page will load faster for my visitors, since the jQuery file will get sent to them from their nearest location, gzipped and compressed. When the visitor visits another site that use the same jQuery [...]]]></description>
			<content:encoded><![CDATA[<p>In most of my project I load the jQuery library from a CDN, either <a href="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">Google</a> or <a href="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js">Microsoft</a>. This ensures that my page will load faster for my visitors, since the jQuery file will get sent to them from their nearest location, gzipped and compressed. When the visitor visits another site that use the same jQuery version from the same CDN, they don&#8217;t need to wait for their browser to download the library since it&#8217;s already in their temporary Internet files.</p>
<p>One of the drawbacks to this approach is when the CDN goes offline or becomes unavailable. Fortunately this has not happened to me &#8211; we should however have a backup plan! While reading through <a href="http://west-wind.com/weblog/posts/459197.aspx">Rick Strahl&#8217;s presentations on jQuery</a> I came across a great little snippet that will use a local copy of the jQuery library if the CDN is down or in some way unavailable.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot; src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js&quot;&gt;&lt;/script&gt;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> jQuery <span style="color: #339933;">==</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span>
	document.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span>unescape<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;%3Cscript src='/scripts/jquery-1.4.2.min.js' type='text/javascript'%3E%3C/script%3E&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Exactly what we need!<strong>Related Posts:</strong>
<ul class="similar-posts">
<li><a href="http://www.frederikvig.com/2009/05/transparent-pngs/" rel="bookmark" title="May 5, 2009">Transparent PNGs</a></li>
<li><a href="http://www.frederikvig.com/2009/10/font-resizing-and-printing-with-jquery/" rel="bookmark" title="October 27, 2009">Font resizing and printing with jQuery</a></li>
<li><a href="http://www.frederikvig.com/2009/12/part-8-preparing-for-launch-create-an-episerver-site-from-scratch/" rel="bookmark" title="December 24, 2009">Part 8: Preparing for launch – Create an EPiServer site from scratch</a></li>
<li><a href="http://www.frederikvig.com/2010/02/visual-studio-2010-episerver-snippets/" rel="bookmark" title="February 11, 2010">Visual Studio 2010 EPiServer Snippets</a></li>
<li><a href="http://www.frederikvig.com/2009/07/episerver-web-controls-property/" rel="bookmark" title="July 5, 2009">EPiServer web controls: Property</a></li>
</ul>
<p><!-- Similar Posts took 20.263 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.frederikvig.com/2010/04/backup-plan-when-loading-the-jquery-library-from-cdn/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Detecting Ajax requests on the server</title>
		<link>http://www.frederikvig.com/2010/04/detecting-ajax-requests-on-the-server/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=detecting-ajax-requests-on-the-server</link>
		<comments>http://www.frederikvig.com/2010/04/detecting-ajax-requests-on-the-server/#comments</comments>
		<pubDate>Tue, 06 Apr 2010 19:27:47 +0000</pubDate>
		<dc:creator>Frederik Vig</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[ASP.NET AJAX]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.frederikvig.com/?p=1222</guid>
		<description><![CDATA[If you use jQuery or ASP.NET AJAX you can easily detect Ajax requests on the server by simply checking for the HTTP_X_REQUESTED_WITH HTTP Header. Both libraries automatically add this to the HTTP Header when they send a request. Here&#8217;s a little code snippet that returns true for Ajax requests. public static bool IsAjaxRequest&#40;&#41; &#123; return [...]]]></description>
			<content:encoded><![CDATA[<p>If you use jQuery or ASP.NET AJAX you can easily detect Ajax requests on the server by simply checking for the HTTP_X_REQUESTED_WITH HTTP Header. Both libraries automatically add this to the HTTP Header when they send a request.</p>
<p>Here&#8217;s a little code snippet that returns true for Ajax requests.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">bool</span> IsAjaxRequest<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">return</span> HttpContext.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Request</span>.<span style="color: #0000FF;">Headers</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;X-Requested-With&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span> <span style="color: #008000;">&amp;&amp;</span> HttpContext.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Request</span>.<span style="color: #0000FF;">Headers</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;X-Requested-With&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;XMLHttpRequest&quot;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The cool thing about this is that you can use this in a base page or a HTTP Module, and customize the returned data. You could for instance return JSON or XML.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Web.UI</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> MyWebApplication
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> BasePage <span style="color: #008000;">:</span> Page
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> OnInit<span style="color: #000000;">&#40;</span>EventArgs e<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">OnInit</span><span style="color: #000000;">&#40;</span>e<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>Helper.<span style="color: #0000FF;">IsAjaxRequest</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                Response.<span style="color: #0000FF;">Headers</span>.<span style="color: #0000FF;">Clear</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                Response.<span style="color: #0000FF;">ContentType</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;application/json&quot;</span><span style="color: #008000;">;</span>
                Response.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>json output<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                Response.<span style="color: #0000FF;">End</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p><strong>Related Posts:</strong>
<ul class="similar-posts">
<li><a href="http://www.frederikvig.com/2010/05/creating-a-contact-form-with-asp-net-mvc/" rel="bookmark" title="May 13, 2010">Creating a contact form with ASP.NET MVC</a></li>
<li><a href="http://www.frederikvig.com/2010/02/getting-the-page-and-episerver-currentpage-object-from-httpcontext/" rel="bookmark" title="February 20, 2010">Getting the Page and EPiServer CurrentPage object from HttpContext</a></li>
<li><a href="http://www.frederikvig.com/2009/05/aspnet-validation-and-jquery/" rel="bookmark" title="May 21, 2009">ASP.NET Validation and jQuery</a></li>
<li><a href="http://www.frederikvig.com/2009/09/creating-a-custom-episerver-paging-control/" rel="bookmark" title="September 20, 2009">Creating a Custom EPiServer Paging Control</a></li>
<li><a href="http://www.frederikvig.com/2009/12/part-8-preparing-for-launch-create-an-episerver-site-from-scratch/" rel="bookmark" title="December 24, 2009">Part 8: Preparing for launch – Create an EPiServer site from scratch</a></li>
</ul>
<p><!-- Similar Posts took 41.449 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.frederikvig.com/2010/04/detecting-ajax-requests-on-the-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Font resizing and printing with jQuery</title>
		<link>http://www.frederikvig.com/2009/10/font-resizing-and-printing-with-jquery/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=font-resizing-and-printing-with-jquery</link>
		<comments>http://www.frederikvig.com/2009/10/font-resizing-and-printing-with-jquery/#comments</comments>
		<pubDate>Tue, 27 Oct 2009 21:00:35 +0000</pubDate>
		<dc:creator>Frederik Vig</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Web design]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.frederikvig.com/?p=360</guid>
		<description><![CDATA[You&#8217;ve probably seen the three A&#8217;s on various websites, especially public sector websites; that you can click on to increase the font size on the web site. I&#8217;m personally not a huge fan of those. In my opinion it is much better to explain to the user how they can use the built in browser [...]]]></description>
			<content:encoded><![CDATA[<p>You&#8217;ve probably seen the three A&#8217;s on various websites, especially public sector websites; that you can click on to increase the font size on the web site. I&#8217;m personally not a huge fan of those. In my opinion it is much better to explain to the user how they can use the built in browser functionality to do this instead. This will also increase the font size on all websites not just the one you created. You could do this by linking to a page with an explanation and screenshots of how to do this in the most popular browsers. </p>
<p>However sometimes I still need to create those three A&#8217;s. Below is a little jQuery plugin that does this.</p>
<p><a href="http://www.frederikvig.com/wp-content/uploads/fontresizing-print-plugin/demo.html">See the demo</a></p>
<h3>Font resizing plugin</h3>
<p>This plugin will create an ordered list with three links to increase the font size. The plugin adds three different classes to the body element, which then can be used to set the base font size.</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">body<span style="color: #6666ff;">.small</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;"><span style="color: #cc66cc;">80</span>%</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
body<span style="color: #6666ff;">.medium</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;"><span style="color: #cc66cc;">120</span>%</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
body<span style="color: #6666ff;">.large</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;"><span style="color: #cc66cc;">140</span>%</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>The plugin code.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">fontresizing</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>customOptions<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> options <span style="color: #339933;">=</span> $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">fontresizing</span>.<span style="color: #660066;">defaultOptions</span><span style="color: #339933;">,</span> customOptions<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> bodyClasses <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span> <span style="color: #339933;">+</span> options.<span style="color: #660066;">smallClass</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">' '</span> <span style="color: #339933;">+</span> options.<span style="color: #660066;">mediumClass</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">' '</span> <span style="color: #339933;">+</span> options.<span style="color: #660066;">largeClass</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;ol class=&quot;'</span> <span style="color: #339933;">+</span> options.<span style="color: #660066;">fontresizingClass</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;&lt;li&gt;&lt;a href=&quot;&quot; class=&quot;'</span> <span style="color: #339933;">+</span> options.<span style="color: #660066;">smallClass</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;A&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;&quot; class=&quot;'</span> <span style="color: #339933;">+</span> options.<span style="color: #660066;">mediumClass</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;A&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;&quot; class=&quot;'</span> <span style="color: #339933;">+</span> options.<span style="color: #660066;">largeClass</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;A&lt;/a&gt;&lt;/li&gt;&lt;/ol&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ol.'</span> <span style="color: #339933;">+</span> options.<span style="color: #660066;">fontresizingClass</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">' a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #003366; font-weight: bold;">var</span> cssClass <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'class'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span>bodyClasses<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span>cssClass<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                createCookie<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'fontresizingClass'</span><span style="color: #339933;">,</span> cssClass<span style="color: #339933;">,</span> options.<span style="color: #660066;">cookieDuration</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #003366; font-weight: bold;">var</span> fontresizingClass <span style="color: #339933;">=</span> readCookie<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'fontresizingClass'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>fontresizingClass <span style="color: #339933;">==</span> options.<span style="color: #660066;">smallClass</span> <span style="color: #339933;">||</span> fontresizingClass <span style="color: #339933;">==</span> options.<span style="color: #660066;">mediumClass</span> <span style="color: #339933;">||</span> fontresizingClass <span style="color: #339933;">==</span> options.<span style="color: #660066;">largeClass</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span>bodyClasses<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span>fontresizingClass<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">// cookie functions http://www.quirksmode.org/js/cookies.html</span>
            <span style="color: #003366; font-weight: bold;">function</span> createCookie<span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #339933;">,</span> value<span style="color: #339933;">,</span> days<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>days<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #003366; font-weight: bold;">var</span> date <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    date.<span style="color: #660066;">setTime</span><span style="color: #009900;">&#40;</span>date.<span style="color: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>days <span style="color: #339933;">*</span> <span style="color: #CC0000;">24</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">60</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">60</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #003366; font-weight: bold;">var</span> expires <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;; expires=&quot;</span> <span style="color: #339933;">+</span> date.<span style="color: #660066;">toGMTString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #003366; font-weight: bold;">var</span> expires <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
                document.<span style="color: #660066;">cookie</span> <span style="color: #339933;">=</span> <span style="color: #000066;">name</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;=&quot;</span> <span style="color: #339933;">+</span> value <span style="color: #339933;">+</span> expires <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;; path=/&quot;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #003366; font-weight: bold;">function</span> readCookie<span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #003366; font-weight: bold;">var</span> nameEQ <span style="color: #339933;">=</span> <span style="color: #000066;">name</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;=&quot;</span><span style="color: #339933;">;</span>
                <span style="color: #003366; font-weight: bold;">var</span> ca <span style="color: #339933;">=</span> document.<span style="color: #660066;">cookie</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">';'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> ca.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #003366; font-weight: bold;">var</span> c <span style="color: #339933;">=</span> ca<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                    <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>c.<span style="color: #660066;">charAt</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">' '</span><span style="color: #009900;">&#41;</span> c <span style="color: #339933;">=</span> c.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> c.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>c.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span>nameEQ<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span> c.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span>nameEQ.<span style="color: #660066;">length</span><span style="color: #339933;">,</span> c.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
&nbsp;
                <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">fontresizing</span>.<span style="color: #660066;">defaultOptions</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
        smallClass<span style="color: #339933;">:</span> <span style="color: #3366CC;">'small'</span><span style="color: #339933;">,</span>
        mediumClass<span style="color: #339933;">:</span> <span style="color: #3366CC;">'medium'</span><span style="color: #339933;">,</span>
        largeClass<span style="color: #339933;">:</span> <span style="color: #3366CC;">'large'</span><span style="color: #339933;">,</span>
        fontresizingClass<span style="color: #339933;">:</span> <span style="color: #3366CC;">'font-resizing'</span><span style="color: #339933;">,</span>
        cookieDuration<span style="color: #339933;">:</span> <span style="color: #CC0000;">365</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h4>Breaking it down</h4>
<p>We start by creating a private scope for our jQuery code. The reason for this is so we don&#8217;t have to worry about conflicts with other libraries.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
...
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>We then add a new method to the jQuery.fn object</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">fontresizing</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>customOptions<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    ...
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Combine the custom options with the default options.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">fontresizing</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>customOptions<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> options <span style="color: #339933;">=</span> $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">fontresizing</span>.<span style="color: #660066;">defaultOptions</span><span style="color: #339933;">,</span> customOptions<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        ...
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">fontresizing</span>.<span style="color: #660066;">defaultOptions</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
        smallClass<span style="color: #339933;">:</span> <span style="color: #3366CC;">'small'</span><span style="color: #339933;">,</span>
        mediumClass<span style="color: #339933;">:</span> <span style="color: #3366CC;">'medium'</span><span style="color: #339933;">,</span>
        largeClass<span style="color: #339933;">:</span> <span style="color: #3366CC;">'large'</span><span style="color: #339933;">,</span>
        fontresizingClass<span style="color: #339933;">:</span> <span style="color: #3366CC;">'font-resizing'</span><span style="color: #339933;">,</span>
        cookieDuration<span style="color: #339933;">:</span> <span style="color: #CC0000;">365</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Next we iterate over the jQuery object or jQuery wrapper set, and return &#8220;this&#8221; (the current jQuery object), so we don&#8217;t break the jQuery chaining functionality.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">fontresizing</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>customOptions<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> options <span style="color: #339933;">=</span> $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">fontresizing</span>.<span style="color: #660066;">defaultOptions</span><span style="color: #339933;">,</span> customOptions<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> bodyClasses <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span> <span style="color: #339933;">+</span> options.<span style="color: #660066;">smallClass</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">' '</span> <span style="color: #339933;">+</span> options.<span style="color: #660066;">mediumClass</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">' '</span> <span style="color: #339933;">+</span> options.<span style="color: #660066;">largeClass</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        ...
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">fontresizing</span>.<span style="color: #660066;">defaultOptions</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
        smallClass<span style="color: #339933;">:</span> <span style="color: #3366CC;">'small'</span><span style="color: #339933;">,</span>
        mediumClass<span style="color: #339933;">:</span> <span style="color: #3366CC;">'medium'</span><span style="color: #339933;">,</span>
        largeClass<span style="color: #339933;">:</span> <span style="color: #3366CC;">'large'</span><span style="color: #339933;">,</span>
        fontresizingClass<span style="color: #339933;">:</span> <span style="color: #3366CC;">'font-resizing'</span><span style="color: #339933;">,</span>
        cookieDuration<span style="color: #339933;">:</span> <span style="color: #CC0000;">365</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now we come to the actual code for creating the links and attaching click events to them.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">fontresizing</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>customOptions<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> options <span style="color: #339933;">=</span> $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">fontresizing</span>.<span style="color: #660066;">defaultOptions</span><span style="color: #339933;">,</span> customOptions<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> bodyClasses <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span> <span style="color: #339933;">+</span> options.<span style="color: #660066;">smallClass</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">' '</span> <span style="color: #339933;">+</span> options.<span style="color: #660066;">mediumClass</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">' '</span> <span style="color: #339933;">+</span> options.<span style="color: #660066;">largeClass</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;ol class=&quot;'</span> <span style="color: #339933;">+</span> options.<span style="color: #660066;">fontresizingClass</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;&lt;li&gt;&lt;a href=&quot;&quot; class=&quot;'</span> <span style="color: #339933;">+</span> options.<span style="color: #660066;">smallClass</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;A&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;&quot; class=&quot;'</span> <span style="color: #339933;">+</span> options.<span style="color: #660066;">mediumClass</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;A&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;&quot; class=&quot;'</span> <span style="color: #339933;">+</span> options.<span style="color: #660066;">largeClass</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;A&lt;/a&gt;&lt;/li&gt;&lt;/ol&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ol.'</span> <span style="color: #339933;">+</span> options.<span style="color: #660066;">fontresizingClass</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">' a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #003366; font-weight: bold;">var</span> cssClass <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'class'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span>bodyClasses<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span>cssClass<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                createCookie<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'fontresizingClass'</span><span style="color: #339933;">,</span> cssClass<span style="color: #339933;">,</span> options.<span style="color: #660066;">cookieDuration</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            ...
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">fontresizing</span>.<span style="color: #660066;">defaultOptions</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
        smallClass<span style="color: #339933;">:</span> <span style="color: #3366CC;">'small'</span><span style="color: #339933;">,</span>
        mediumClass<span style="color: #339933;">:</span> <span style="color: #3366CC;">'medium'</span><span style="color: #339933;">,</span>
        largeClass<span style="color: #339933;">:</span> <span style="color: #3366CC;">'large'</span><span style="color: #339933;">,</span>
        fontresizingClass<span style="color: #339933;">:</span> <span style="color: #3366CC;">'font-resizing'</span><span style="color: #339933;">,</span>
        cookieDuration<span style="color: #339933;">:</span> <span style="color: #CC0000;">365</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Lastly we have the cookie functions to read and create the cookie to hold the font size choices the user has made.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">...
            <span style="color: #003366; font-weight: bold;">var</span> fontresizingClass <span style="color: #339933;">=</span> readCookie<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'fontresizingClass'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>fontresizingClass <span style="color: #339933;">==</span> options.<span style="color: #660066;">smallClass</span> <span style="color: #339933;">||</span> fontresizingClass <span style="color: #339933;">==</span> options.<span style="color: #660066;">mediumClass</span> <span style="color: #339933;">||</span> fontresizingClass <span style="color: #339933;">==</span> options.<span style="color: #660066;">largeClass</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span>bodyClasses<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span>fontresizingClass<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">// cookie functions from http://www.quirksmode.org/js/cookies.html</span>
            <span style="color: #003366; font-weight: bold;">function</span> createCookie<span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #339933;">,</span> value<span style="color: #339933;">,</span> days<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>days<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #003366; font-weight: bold;">var</span> date <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    date.<span style="color: #660066;">setTime</span><span style="color: #009900;">&#40;</span>date.<span style="color: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>days <span style="color: #339933;">*</span> <span style="color: #CC0000;">24</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">60</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">60</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #003366; font-weight: bold;">var</span> expires <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;; expires=&quot;</span> <span style="color: #339933;">+</span> date.<span style="color: #660066;">toGMTString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #003366; font-weight: bold;">var</span> expires <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
                document.<span style="color: #660066;">cookie</span> <span style="color: #339933;">=</span> <span style="color: #000066;">name</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;=&quot;</span> <span style="color: #339933;">+</span> value <span style="color: #339933;">+</span> expires <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;; path=/&quot;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #003366; font-weight: bold;">function</span> readCookie<span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #003366; font-weight: bold;">var</span> nameEQ <span style="color: #339933;">=</span> <span style="color: #000066;">name</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;=&quot;</span><span style="color: #339933;">;</span>
                <span style="color: #003366; font-weight: bold;">var</span> ca <span style="color: #339933;">=</span> document.<span style="color: #660066;">cookie</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">';'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> ca.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #003366; font-weight: bold;">var</span> c <span style="color: #339933;">=</span> ca<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                    <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>c.<span style="color: #660066;">charAt</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">' '</span><span style="color: #009900;">&#41;</span> c <span style="color: #339933;">=</span> c.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> c.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>c.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span>nameEQ<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span> c.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span>nameEQ.<span style="color: #660066;">length</span><span style="color: #339933;">,</span> c.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
&nbsp;
                <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
...</pre></div></div>

<h4>Usage</h4>
<p>To use the plugin simply add a reference to the jQuery library file, and copy the code into your page. After you&#8217;ve done that you can use it by calling the fontresizing method</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// With default options</span>
jQuery<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#tools'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fontresizing</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// With custom options</span>
jQuery<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#tools'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fontresizing</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        smallClass<span style="color: #339933;">:</span> <span style="color: #3366CC;">'small'</span><span style="color: #339933;">,</span>
        mediumClass<span style="color: #339933;">:</span> <span style="color: #3366CC;">'medium'</span><span style="color: #339933;">,</span>
        largeClass<span style="color: #339933;">:</span> <span style="color: #3366CC;">'large'</span><span style="color: #339933;">,</span>
        fontresizingClass<span style="color: #339933;">:</span> <span style="color: #3366CC;">'font-resizing'</span><span style="color: #339933;">,</span>
        cookieDuration<span style="color: #339933;">:</span> <span style="color: #CC0000;">365</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Print plugin</h3>
<p>Another thing that many clients ask for is a print link. Below is a little jQuery plugin that creates the link and attaches a click event that triggers the browsers print dialog.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $.<span style="color: #660066;">fn</span>.<span style="color: #000066;">print</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>customOptions<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> options <span style="color: #339933;">=</span> $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> $.<span style="color: #660066;">fn</span>.<span style="color: #000066;">print</span>.<span style="color: #660066;">defaultOptions</span><span style="color: #339933;">,</span> customOptions<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;a href=&quot;&quot; class=&quot;'</span> <span style="color: #339933;">+</span> options.<span style="color: #660066;">printClass</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;'</span> <span style="color: #339933;">+</span> options.<span style="color: #660066;">printText</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/a&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a.'</span> <span style="color: #339933;">+</span> options.<span style="color: #660066;">printClass</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                window.<span style="color: #000066;">print</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    $.<span style="color: #660066;">fn</span>.<span style="color: #000066;">print</span>.<span style="color: #660066;">defaultOptions</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
        printClass<span style="color: #339933;">:</span> <span style="color: #3366CC;">'print'</span><span style="color: #339933;">,</span>
        printText<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Print'</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Here we also generate the link with JavaScript, so that users who don&#8217;t have JavaScript wont see it &#8211; since they cannot use it anyway. When you click on the link you&#8217;ll see the print dialog; use this with CSS to create a printer friendly version of your page.</p>
<p>To create CSS rules for print either use the <a href="http://reference.sitepoint.com/css/at-media">@media</a></p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #a1a100;">@media print {</span>
  <span style="color: #808080; font-style: italic;">/* CSS rules here */</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>or link to an external CSS file and add the media=&#8221;print&#8221; attribute.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;styles/print.css&quot;</span> <span style="color: #000066;">media</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;print&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span></pre></div></div>

<h4>Usage</h4>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// With default options</span>
jQuery<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#tools'</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066;">print</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// With custom options</span>
jQuery<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#tools'</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066;">print</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        printText<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Print this page'</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Download the code or <a href="http://www.frederikvig.com/wp-content/uploads/fontresizing-print-plugin/demo.html">see the demo</a></p>
<ul>
<li><a href="http://www.frederikvig.com/wp-content/uploads/fontresizing-print-plugin/jquery.fontresizing.js">jquery.fontresizing.js</a></li>
<li><a href="http://www.frederikvig.com/wp-content/uploads/fontresizing-print-plugin/jquery.print.js">jquery.print.js</a></li>
</ul>
<p><strong>Related Posts:</strong>
<ul class="similar-posts">
<li><a href="http://www.frederikvig.com/2009/07/asp-net-web-forms-and-jquery-thickbox-plugin/" rel="bookmark" title="July 13, 2009">ASP.NET web forms and jQuery Thickbox plugin</a></li>
<li><a href="http://www.frederikvig.com/2009/05/aspnet-validation-and-jquery/" rel="bookmark" title="May 21, 2009">ASP.NET Validation and jQuery</a></li>
<li><a href="http://www.frederikvig.com/2009/05/transparent-pngs/" rel="bookmark" title="May 5, 2009">Transparent PNGs</a></li>
<li><a href="http://www.frederikvig.com/2009/05/measuring-javascript-performance/" rel="bookmark" title="May 10, 2009">Measuring JavaScript performance</a></li>
<li><a href="http://www.frederikvig.com/2010/04/backup-plan-when-loading-the-jquery-library-from-cdn/" rel="bookmark" title="April 20, 2010">Backup plan when loading the jQuery library from CDN</a></li>
</ul>
<p><!-- Similar Posts took 17.130 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.frederikvig.com/2009/10/font-resizing-and-printing-with-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Extending search field with suggestion box</title>
		<link>http://www.frederikvig.com/2009/09/extending-search-field-with-suggestion-box/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=extending-search-field-with-suggestion-box</link>
		<comments>http://www.frederikvig.com/2009/09/extending-search-field-with-suggestion-box/#comments</comments>
		<pubDate>Thu, 17 Sep 2009 15:20:58 +0000</pubDate>
		<dc:creator>Frederik Vig</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Code Snippet]]></category>
		<category><![CDATA[EPiServer]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.frederikvig.com/?p=212</guid>
		<description><![CDATA[Disclaimer: In this example I&#8217;ve kept the code simple to make it easier to read and to give you an idea of how you might approach something like this with EPiServer. This code should not be used in production scenarios since it will use a lot of resources. Use at own risk . Update &#8211; [...]]]></description>
			<content:encoded><![CDATA[<p>Disclaimer: In this example I&#8217;ve kept the code simple to make it easier to read and to give you an idea of how you might approach something like this with EPiServer. This code should not be used in production scenarios since it will use a lot of resources. Use at own risk <img src='http://www.frederikvig.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<h3>Update &#8211; 12.10.2009</h3>
<p>Updated production code with a new custom filter that removes pages that the Everyone role doesn&#8217;t have access to.<br />
Also updated the Get caching method. See <a href="http://www.frederikvig.com/2009/09/extending-search-field-with-suggestion-box/comment-page-1/#comment-38">comments below for further details</a>.</p>
<h3>Update &#8211; 19.09.2009</h3>
<p>Added example of production code you might consider. </p>
<p>We&#8217;ve all used Google Suggest before. When you start typing a word or sentence, Google comes up with suggestions to what we might be searching for.<br />
<img src="http://www.frederikvig.com/wp-content/uploads/suggest/google-suggest.png" alt="Example of Google Suggest" /><br />
This is to help users quickly find what they want. </p>
<p>In this blog post we&#8217;re going to do the same by extending QuickSearch that ships with the EPiServer Public Templates. </p>
<p>I decided to use <a href="http://www.jquery.com/">jQuery</a> and the <a href="http://plugins.jquery.com/project/autocompletex">AutoComplete</a> plugin for this, but you can use any script you like (most are pretty similar).</p>
<h3>The result</h3>
<p><img src="http://www.frederikvig.com/wp-content/uploads/suggest/episerver-suggest.png" alt="Example of search suggest" /></p>
<h3>The Code</h3>
<p>First start by modifying the QuickSearch.ascx:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>Panel runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> CssClass<span style="color: #008000;">=</span><span style="color: #666666;">&quot;QuickSearchArea&quot;</span> DefaultButton<span style="color: #008000;">=</span><span style="color: #666666;">&quot;SearchButton&quot;</span><span style="color: #008000;">&gt;</span>
	<span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>Label ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;SearchLabel&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> AssociatedControlID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;SearchText&quot;</span> CssClass<span style="color: #008000;">=</span><span style="color: #666666;">&quot;hidden&quot;</span> Text<span style="color: #008000;">=</span><span style="color: #666666;">&quot;&lt;%$ Resources: EPiServer, navigation.search %&gt;&quot;</span> <span style="color: #008000;">/&gt;</span>
    <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>TextBox ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;SearchText&quot;</span> TabIndex<span style="color: #008000;">=</span><span style="color: #666666;">&quot;1&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> CssClass<span style="color: #008000;">=</span><span style="color: #666666;">&quot;quickSearchField&quot;</span> autocomplete<span style="color: #008000;">=</span><span style="color: #666666;">&quot;off&quot;</span> <span style="color: #008000;">/&gt;</span>
    <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>ImageButton ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;SearchButton&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> ImageUrl<span style="color: #008000;">=</span><span style="color: #666666;">&quot;~/Templates/Public/Images/MainMenuSearchButton.png&quot;</span> ToolTip<span style="color: #008000;">=</span><span style="color: #666666;">&quot;&lt;%$ Resources: EPiServer, navigation.search %&gt;&quot;</span> CausesValidation<span style="color: #008000;">=</span><span style="color: #666666;">&quot;false&quot;</span> CssClass<span style="color: #008000;">=</span><span style="color: #666666;">&quot;quickSearchButton&quot;</span> OnClick<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Search_Click&quot;</span> <span style="color: #008000;">/&gt;</span>
<span style="color: #008000;">&lt;/</span>asp<span style="color: #008000;">:</span>Panel<span style="color: #008000;">&gt;</span>
&nbsp;
<span style="color: #008000;">&lt;</span>script type<span style="color: #008000;">=</span><span style="color: #666666;">&quot;text/javascript&quot;</span><span style="color: #008000;">&gt;</span>
<span style="color: #008080; font-style: italic;">//&lt;![CDATA[ </span>
    $<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;#&lt;%=SearchText.ClientID %&gt;&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">autocomplete</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;/path/to/file/SearchSuggest.aspx&quot;</span>, <span style="color: #000000;">&#123;</span>
        minChars<span style="color: #008000;">:</span> <span style="color: #FF0000;">2</span>,
        max<span style="color: #008000;">:</span> <span style="color: #FF0000;">10</span>
    <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">//]]&gt; </span>
<span style="color: #008000;">&lt;/</span>script<span style="color: #008000;">&gt;</span></pre></div></div>

<p>A few things to note. I removed the default text from the TextBox and added the attribute autocomplete=&#8221;off&#8221;, this is to ensure that the users browser does not store the information (<a href="https://developer.mozilla.org/en/How_to_Turn_Off_Form_Autocompletion">How to Turn Off Form Autocompletion</a>). I then attached the AutoComplete plugin to the TextBox and changed a few of its default properties. </p>
<p>As you can see there is not much code that is needed when mostly using the default settings. You can of course do a lot more. Browse through the <a href="http://docs.jquery.com/Plugins/Autocomplete">AutoComplete plugin documentation</a> and see the <a href="http://view.jquery.com/trunk/plugins/autocomplete/demo/">demos</a> to get a few ideas.</p>
<p>Now for the main part, SearchSuggestion.aspx. This is just an empty web forms page with all the code in the code-behind:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Text</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Web</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">EPiServer</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">EPiServer.Core</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">EPiServer.Filters</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">EPiServer.Security</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> FV.<span style="color: #0000FF;">Templates</span>.<span style="color: #0000FF;">FV</span>.<span style="color: #0000FF;">Pages</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">partial</span> <span style="color: #FF0000;">class</span> SearchSuggest <span style="color: #008000;">:</span> TemplatePage
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> OnLoad<span style="color: #000000;">&#40;</span>EventArgs e<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// #1</span>
            <span style="color: #FF0000;">string</span> query <span style="color: #008000;">=</span> HttpUtility.<span style="color: #0000FF;">HtmlEncode</span><span style="color: #000000;">&#40;</span>Request.<span style="color: #0000FF;">QueryString</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;q&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #FF0000;">string</span> limit <span style="color: #008000;">=</span> HttpUtility.<span style="color: #0000FF;">HtmlEncode</span><span style="color: #000000;">&#40;</span>Request.<span style="color: #0000FF;">QueryString</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;limit&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>query<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                var sb <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StringBuilder<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// #2</span>
                var criterias <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PropertyCriteriaCollection<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                var queryCriteria <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PropertyCriteria
                                        <span style="color: #000000;">&#123;</span>
                                            Condition <span style="color: #008000;">=</span> CompareCondition.<span style="color: #0000FF;">StartsWith</span>,
                                            Name <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;PageName&quot;</span>,
                                            Value <span style="color: #008000;">=</span> query,
                                            Type <span style="color: #008000;">=</span> PropertyDataType.<span style="color: #FF0000;">String</span>,
                                            Required <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</span>
                                        <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
                var pubCriteria <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PropertyCriteria
                                      <span style="color: #000000;">&#123;</span>
                                          Condition <span style="color: #008000;">=</span> CompareCondition.<span style="color: #0000FF;">Equal</span>,
                                          Name <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;PagePendingPublish&quot;</span>,
                                          Value <span style="color: #008000;">=</span> <span style="color: #0600FF;">false</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>,
                                          Type <span style="color: #008000;">=</span> PropertyDataType.<span style="color: #0000FF;">Boolean</span>,
                                          Required <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</span>
                                      <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
                criterias.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>queryCriteria<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                criterias.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>pubCriteria<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// #3</span>
                var pages <span style="color: #008000;">=</span> DataFactory.<span style="color: #0000FF;">Instance</span>.<span style="color: #0000FF;">FindPagesWithCriteria</span><span style="color: #000000;">&#40;</span>PageReference.<span style="color: #0000FF;">StartPage</span>, criterias, CurrentPage.<span style="color: #0000FF;">LanguageBranch</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #FF0000;">int</span> tempLimit <span style="color: #008000;">=</span> Convert.<span style="color: #0000FF;">ToInt32</span><span style="color: #000000;">&#40;</span>limit<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #FF0000;">int</span> pagesCount <span style="color: #008000;">=</span> pages.<span style="color: #0000FF;">Count</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// #4</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>tempLimit <span style="color: #008000;">&gt;</span> pagesCount<span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    tempLimit <span style="color: #008000;">=</span> pagesCount<span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// #5</span>
                <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> tempLimit<span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    sb.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span>pages<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">PageName</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                    sb.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span>Environment.<span style="color: #0000FF;">NewLine</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// #6</span>
                Response.<span style="color: #0000FF;">AddHeader</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Content-Type&quot;</span>, <span style="color: #666666;">&quot;text/html&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                Response.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>sb.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                Response.<span style="color: #0000FF;">End</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<ul>
<li>In #1 we retrieve the user input and the limit (which we set earlier with the max property in QuickSearch.ascx), we then HTML encode it (something you should always do when receiving user input).</li>
<li>In #2 we build the criteria collection with our search options. We search for pages that start with what the user typed in and then make sure that only published pages are returned.</li>
<li>In #3 we do the actual searching with the <a href="http://sdk.episerver.com/library/cms6/html/Overload_EPiServer_DataFactory_FindPagesWithCriteria.htm">FindPagesWithCriteria method</a> where we also set the current language branch (only return pages in that language).</li>
<li>In #4 we make sure that we don&#8217;t have less pages than the limit, if so we set the pages count to be the limit.</li>
<li>In #5 we go through pages (PageDataCollection) and add the PageName to sb (StringBuilder).</li>
<li>In #6 we return the result back</li>
</ul>
<p>Below is the code for SearchSuggest.aspx.cs updated to use the SearchDataSource instead of FindPagesWithCriteria (this will not only search the PageName but all properties that are search able):</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Text</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Web</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Web.UI</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">EPiServer</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">EPiServer.Core</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">EPiServer.Security</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">EPiServer.Web.WebControls</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> FV.<span style="color: #0000FF;">Templates</span>.<span style="color: #0000FF;">FV</span>.<span style="color: #0000FF;">Pages</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">partial</span> <span style="color: #FF0000;">class</span> SearchSuggest <span style="color: #008000;">:</span> TemplatePage
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> OnLoad<span style="color: #000000;">&#40;</span>EventArgs e<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #FF0000;">string</span> query <span style="color: #008000;">=</span> HttpUtility.<span style="color: #0000FF;">HtmlEncode</span><span style="color: #000000;">&#40;</span>Request.<span style="color: #0000FF;">QueryString</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;q&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #FF0000;">string</span> limit <span style="color: #008000;">=</span> HttpUtility.<span style="color: #0000FF;">HtmlEncode</span><span style="color: #000000;">&#40;</span>Request.<span style="color: #0000FF;">QueryString</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;limit&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>query<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                var sb <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StringBuilder<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                var searchDataSource <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SearchDataSource
                <span style="color: #000000;">&#123;</span>
                    SearchQuery <span style="color: #008000;">=</span> query,
                    AccessLevel <span style="color: #008000;">=</span> AccessLevel.<span style="color: #0000FF;">Read</span>,
                    PublishedStatus <span style="color: #008000;">=</span> PagePublishedStatus.<span style="color: #0000FF;">Published</span>,
                    PageLink <span style="color: #008000;">=</span> PageReference.<span style="color: #0000FF;">StartPage</span>,
                    LanguageBranches <span style="color: #008000;">=</span> CurrentPage.<span style="color: #0000FF;">LanguageBranch</span>,
                    MaxCount <span style="color: #008000;">=</span> Convert.<span style="color: #0000FF;">ToInt32</span><span style="color: #000000;">&#40;</span>limit<span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>PageData page <span style="color: #0600FF;">in</span> searchDataSource.<span style="color: #0000FF;">Select</span><span style="color: #000000;">&#40;</span>DataSourceSelectArguments.<span style="color: #0000FF;">Empty</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    sb.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span>page.<span style="color: #0000FF;">PageName</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                    sb.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span>Environment.<span style="color: #0000FF;">NewLine</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
&nbsp;
                Response.<span style="color: #0000FF;">AddHeader</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Content-Type&quot;</span>, <span style="color: #666666;">&quot;text/html&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                Response.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>sb.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                Response.<span style="color: #0000FF;">End</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<h3>Production code</h3>
<p>Like I said in the beginning, this code should not be used in production scenarios since it goes through the entire site searching for matching PageNames each time there is a request. </p>
<p>I&#8217;ve added some example code of how you might approach this in a production scenario. Basically I just use the FindPagesWithCriteria method to find all the pages and then store the PageName of each in an array that I then store in the cache for easy access.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Linq</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Text</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Web</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">EPiServer</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">EPiServer.Core</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">EPiServer.Filters</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">EPiServer.Security</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">FV.Templates.FV.Filters</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> FV.<span style="color: #0000FF;">Templates</span>.<span style="color: #0000FF;">FV</span>.<span style="color: #0000FF;">Pages</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">partial</span> <span style="color: #FF0000;">class</span> ProductionSearchSuggestion <span style="color: #008000;">:</span> TemplatePage
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> OnLoad<span style="color: #000000;">&#40;</span>EventArgs e<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #FF0000;">string</span> query <span style="color: #008000;">=</span> HttpUtility.<span style="color: #0000FF;">HtmlEncode</span><span style="color: #000000;">&#40;</span>Request.<span style="color: #0000FF;">QueryString</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;q&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #FF0000;">string</span> limit <span style="color: #008000;">=</span> HttpUtility.<span style="color: #0000FF;">HtmlEncode</span><span style="color: #000000;">&#40;</span>Request.<span style="color: #0000FF;">QueryString</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;limit&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>query<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                var sb <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StringBuilder<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> pageNames<span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">string</span> pageNamesCacheKey <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;AllPageNames&quot;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>Get<span style="color: #000000;">&#40;</span>pageNamesCacheKey, <span style="color: #0600FF;">out</span> pageNames<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    var criterias <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PropertyCriteriaCollection<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                    var pubCriteria <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PropertyCriteria
                    <span style="color: #000000;">&#123;</span>
                        Condition <span style="color: #008000;">=</span> CompareCondition.<span style="color: #0000FF;">Equal</span>,
                        Name <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;PagePendingPublish&quot;</span>,
                        Value <span style="color: #008000;">=</span> <span style="color: #0600FF;">false</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>,
                        Type <span style="color: #008000;">=</span> PropertyDataType.<span style="color: #0000FF;">Boolean</span>,
                        Required <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</span>
                    <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
                    criterias.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>pubCriteria<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                    var pages <span style="color: #008000;">=</span> DataFactory.<span style="color: #0000FF;">Instance</span>.<span style="color: #0000FF;">FindPagesWithCriteria</span><span style="color: #000000;">&#40;</span>PageReference.<span style="color: #0000FF;">StartPage</span>, criterias, CurrentPage.<span style="color: #0000FF;">LanguageBranch</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                    <span style="color: #008000;">new</span> FilterRole<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Everyone&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Filter</span><span style="color: #000000;">&#40;</span>pages<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                    var pagesCount <span style="color: #008000;">=</span> pages.<span style="color: #0000FF;">Count</span><span style="color: #008000;">;</span>
                    pageNames <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span>pagesCount<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
&nbsp;
                    <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> pagesCount<span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
                    <span style="color: #000000;">&#123;</span>
                        pageNames<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> pages<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">PageName</span><span style="color: #008000;">;</span>
                    <span style="color: #000000;">&#125;</span>
&nbsp;
                    Add<span style="color: #000000;">&#40;</span>pageNames, pageNamesCacheKey<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
&nbsp;
                <span style="color: #FF0000;">int</span> tempLimit <span style="color: #008000;">=</span> Convert.<span style="color: #0000FF;">ToInt32</span><span style="color: #000000;">&#40;</span>limit<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                var result <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span>tempLimit<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
                <span style="color: #FF0000;">int</span> resultCounter <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> pageNames.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>resultCounter <span style="color: #008000;">&lt;=</span> tempLimit <span style="color: #008000;">&amp;&amp;</span> pageNames<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">ToLower</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">StartsWith</span><span style="color: #000000;">&#40;</span>query.<span style="color: #0000FF;">ToLower</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #008000;">!</span>result.<span style="color: #0000FF;">Contains</span><span style="color: #000000;">&#40;</span>pageNames<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                    <span style="color: #000000;">&#123;</span>
                        result<span style="color: #000000;">&#91;</span>resultCounter<span style="color: #008000;">++</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> pageNames<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
                    <span style="color: #000000;">&#125;</span>
                <span style="color: #000000;">&#125;</span>
&nbsp;
                <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> resultCounter<span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    sb.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span>result<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                    sb.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span>Environment.<span style="color: #0000FF;">NewLine</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
&nbsp;
                Response.<span style="color: #0000FF;">AddHeader</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Content-Type&quot;</span>, <span style="color: #666666;">&quot;text/html&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                Response.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>sb.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                Response.<span style="color: #0000FF;">End</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// Caching methods from http://johnnycoder.com/blog/2008/12/10/c-cache-helper-class/</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> Exists<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> key<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> HttpContext.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Cache</span><span style="color: #000000;">&#91;</span>key<span style="color: #000000;">&#93;</span> <span style="color: #008000;">!=</span> null<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> Get<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> key, <span style="color: #0600FF;">out</span> T value<span style="color: #000000;">&#41;</span> where T<span style="color: #008000;">:</span><span style="color: #FF0000;">class</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">try</span>
            <span style="color: #000000;">&#123;</span>
                value <span style="color: #008000;">=</span> HttpContext.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Cache</span><span style="color: #000000;">&#91;</span>key<span style="color: #000000;">&#93;</span> <span style="color: #0600FF;">as</span> T<span style="color: #008000;">;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>value <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    value <span style="color: #008000;">=</span> <span style="color: #0600FF;">default</span><span style="color: #000000;">&#40;</span>T<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                    <span style="color: #0600FF;">return</span> false<span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">catch</span>
            <span style="color: #000000;">&#123;</span>
                value <span style="color: #008000;">=</span> <span style="color: #0600FF;">default</span><span style="color: #000000;">&#40;</span>T<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">return</span> false<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
&nbsp;
            <span style="color: #0600FF;">return</span> true<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Add<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>T o, <span style="color: #FF0000;">string</span> key<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            HttpContext.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Cache</span>.<span style="color: #0000FF;">Insert</span><span style="color: #000000;">&#40;</span>
                key,
                o,
                <span style="color: #0600FF;">null</span>,
                DateTime.<span style="color: #0000FF;">Now</span>.<span style="color: #0000FF;">AddMinutes</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">3600</span><span style="color: #000000;">&#41;</span>,
                <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">Caching</span></span>.<span style="color: #0000FF;">Cache</span>.<span style="color: #0000FF;">NoSlidingExpiration</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The FilterRole class</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">EPiServer.Core</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">EPiServer.Filters</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> FV.<span style="color: #0000FF;">Templates</span>.<span style="color: #0000FF;">FV</span>.<span style="color: #0000FF;">Filters</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> FilterRole <span style="color: #008000;">:</span> IPageFilter
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> RoleName
        <span style="color: #000000;">&#123;</span>
            get<span style="color: #008000;">;</span>
            set<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> FilterRole<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> FilterRole<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> roleName<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">RoleName</span> <span style="color: #008000;">=</span> roleName<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Filter<span style="color: #000000;">&#40;</span>PageDataCollection pages<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> pages.<span style="color: #0000FF;">Count</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&gt;=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i<span style="color: #008000;">--</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #FF0000;">bool</span> isMatch <span style="color: #008000;">=</span> false<span style="color: #008000;">;</span>
                var acl <span style="color: #008000;">=</span> pages<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">ACL</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>acl <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var role <span style="color: #0600FF;">in</span> acl<span style="color: #000000;">&#41;</span>
                    <span style="color: #000000;">&#123;</span>
                        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>role.<span style="color: #0000FF;">Key</span> <span style="color: #008000;">==</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">RoleName</span><span style="color: #000000;">&#41;</span>
                        <span style="color: #000000;">&#123;</span>
                            isMatch <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
                        <span style="color: #000000;">&#125;</span>
                    <span style="color: #000000;">&#125;</span>
                <span style="color: #000000;">&#125;</span>
&nbsp;
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>isMatch<span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    pages.<span style="color: #0000FF;">RemoveAt</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Filter<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, FilterEventArgs e<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Filter</span><span style="color: #000000;">&#40;</span>e.<span style="color: #0000FF;">Pages</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> ShouldFilter<span style="color: #000000;">&#40;</span>PageData page<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> NotImplementedException<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p><a href="http://www.frederikvig.com/wp-content/uploads/suggest/SearchSuggest.zip">Download the code</a><strong>Related Posts:</strong>
<ul class="similar-posts">
<li><a href="http://www.frederikvig.com/2009/06/using-multiple-forms-on-an-asp-net-web-forms-page/" rel="bookmark" title="June 21, 2009">Using multiple forms on an ASP.NET web forms page</a></li>
<li><a href="http://www.frederikvig.com/2009/10/episerver-findpageswithcriteria-and-findallpageswithcriteria/" rel="bookmark" title="October 21, 2009">EPiServer FindPagesWithCriteria and FindAllPagesWithCriteria</a></li>
<li><a href="http://www.frederikvig.com/2009/05/aspnet-validation-and-jquery/" rel="bookmark" title="May 21, 2009">ASP.NET Validation and jQuery</a></li>
<li><a href="http://www.frederikvig.com/2009/09/creating-a-custom-episerver-paging-control/" rel="bookmark" title="September 20, 2009">Creating a Custom EPiServer Paging Control</a></li>
<li><a href="http://www.frederikvig.com/2009/12/part-5-creating-the-search-page-create-an-episerver-site-from-scratch/" rel="bookmark" title="December 16, 2009">Part 5: Creating the search page – Create an EPiServer site from scratch</a></li>
</ul>
<p><!-- Similar Posts took 14.277 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.frederikvig.com/2009/09/extending-search-field-with-suggestion-box/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>ASP.NET web forms and jQuery Thickbox plugin</title>
		<link>http://www.frederikvig.com/2009/07/asp-net-web-forms-and-jquery-thickbox-plugin/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=asp-net-web-forms-and-jquery-thickbox-plugin</link>
		<comments>http://www.frederikvig.com/2009/07/asp-net-web-forms-and-jquery-thickbox-plugin/#comments</comments>
		<pubDate>Mon, 13 Jul 2009 19:55:48 +0000</pubDate>
		<dc:creator>Frederik Vig</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.frederikvig.com/?p=113</guid>
		<description><![CDATA[Update Thickbox is no longer supported. Use another of jQuery&#8217;s overlay/lightbox plugins. This post will remain for legacy purposes only. I&#8217;m very found of jQuery, and use the Thickbox plugin a lot in my project. I&#8217;ve not used it much for images, but rather for content. To retrieve content you have three options. Inline content [...]]]></description>
			<content:encoded><![CDATA[<h3>Update</h3>
<p><a href="http://jquery.com/demo/thickbox/">Thickbox</a> is no longer supported. Use another of <a href="http://plugins.jquery.com/project/Plugins/category/43">jQuery&#8217;s overlay/lightbox plugins</a>. This post will remain for legacy purposes only.</p>
<p>I&#8217;m very found of <a href="http://www.jquery.com/">jQuery</a>, and use the <a href="http://jquery.com/demo/thickbox/">Thickbox</a> plugin a lot in my project. I&#8217;ve not used it much for images, but rather for content. To retrieve content you have three options.</p>
<ul>
<li>Inline content</li>
<li>Iframed content</li>
<li>Ajax content</li>
</ul>
<h3>Inline content</h3>
<p>This is pretty straight forward, you have some content on the same page that you&#8217;d like to display inside the Thickbox. This one I use a lot, since it is easy to make accessible for search engines, screen readers and other devices. You simply hide the content with JavaScript (in jQuery you can use the hide function: $(selector).hide();), and then trigger opening the content inside the Thickbox with either a link or button.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#TB_inline?height=155&amp;amp;width=300&amp;amp;inlineId=contentId&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;thickbox&quot;</span>&gt;</span>Show hidden content.<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span></pre></div></div>

<h3>Iframe content</h3>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Default.aspx?keepThis=true&amp;amp;TB_iframe=true&amp;amp;height=300&amp;amp;width=500&quot;</span> <span style="color: #000066;">title</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;thickbox title&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;thickbox&quot;</span>&gt;</span>Iframe content<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span></pre></div></div>

<p>The nice thing about this is that you can easily include existing pages inside the Thickbox. Another thing to note is that users without JavaScript, like search engines, still can navigate the content, since the a element&#8217;s href attribute points to the page. This is also the easiest way to still have functionality that you have inside your ASP.NET web form page like Postbacks, sessions etc.</p>
<h3>Ajax content</h3>
<p>Like with iframed content people without JavaScript will still be able get to the content.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ajaxContent.aspx?height=300&amp;amp;width=300&quot;</span> <span style="color: #000066;">title</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;thickbox title&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;thickbox&quot;</span>&gt;</span>Ajax conten<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span></pre></div></div>

<p>The one problem I&#8217;ve had in the past with this method is when posting data back to the server with ASP.NET web forms (same problem with inline content). Until recently I always used iframed content when having to deal with form controls. The reason being that the content inside the Thickbox got added outside the form element on the page (right before the closing body tag).</p>
<p><img src="http://www.frederikvig.com/wp-content/uploads/thickbox1.png" alt="Thickbox code right before closing body tag" /></p>
<p>To fix this you need to change this line of code inside the thickbox.js file.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;body&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;div id='TB_overlay'&gt;&lt;/div&gt;&lt;div id='TB_window'&gt;&lt;/div&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Add form:first to the selector to target the first form element on the page.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;body form:first&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;div id='TB_overlay'&gt;&lt;/div&gt;&lt;div id='TB_window'&gt;&lt;/div&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><img src="http://www.frederikvig.com/wp-content/uploads/thickbox2.png" alt="Thickbox code inside the form tag now" /></p>
<p>Now the form controls will work and you&#8217;ll be able to have the user fill out the form inside the Thickbox and post it back to the server.</p>
<p>The Thickbox is also very easy to style and extend, below are some screen shots of sites where I&#8217;ve used it.</p>
<p><a href="http://www.frederikvig.com/wp-content/uploads/2009/03/img_01-mar-30-2208.png"><img class="alignnone size-medium wp-image-33" title="thickbox screen shot" src="http://www.frederikvig.com/wp-content/uploads/2009/03/img_01-mar-30-2208-300x146.png" alt="thickbox screen shot" width="300" height="146" /></a></p>
<p><a href="http://www.frederikvig.com/wp-content/uploads/2009/03/img_02-mar-30-2208.png"><img class="alignnone size-medium wp-image-34" title="Thickbox screen shot" src="http://www.frederikvig.com/wp-content/uploads/2009/03/img_02-mar-30-2208-300x265.png" alt="Thickbox screen shot" width="300" height="265" /></a></p>
<h3>Other resources</h3>
<ul>
<li><a href="http://mattberseth.com/blog/2008/06/masterdetail_with_the_gridview_1.html">Master-Detail with the GridView, DetailsView and jQuery&#8217;s ThickBox</a></li>
<li><a href="http://jquery.com/demo/thickbox/">Thickbox plugin</a></li>
<li><a href="http://www.jquery.com">jQuery</a></li>
</ul>
<p><strong>Related Posts:</strong>
<ul class="similar-posts">
<li><a href="http://www.frederikvig.com/2009/05/transparent-pngs/" rel="bookmark" title="May 5, 2009">Transparent PNGs</a></li>
<li><a href="http://www.frederikvig.com/2009/06/using-multiple-forms-on-an-asp-net-web-forms-page/" rel="bookmark" title="June 21, 2009">Using multiple forms on an ASP.NET web forms page</a></li>
<li><a href="http://www.frederikvig.com/2009/05/aspnet-validation-and-jquery/" rel="bookmark" title="May 21, 2009">ASP.NET Validation and jQuery</a></li>
<li><a href="http://www.frederikvig.com/2009/11/specify-your-preferred-external-url-in-episerver/" rel="bookmark" title="November 7, 2009">Specify your preferred external URL in EPiServer</a></li>
<li><a href="http://www.frederikvig.com/2009/05/css-tricks-part-1/" rel="bookmark" title="May 31, 2009">CSS tricks &#8211; part 1</a></li>
</ul>
<p><!-- Similar Posts took 9.897 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.frederikvig.com/2009/07/asp-net-web-forms-and-jquery-thickbox-plugin/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>ASP.NET Validation and jQuery</title>
		<link>http://www.frederikvig.com/2009/05/aspnet-validation-and-jquery/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=aspnet-validation-and-jquery</link>
		<comments>http://www.frederikvig.com/2009/05/aspnet-validation-and-jquery/#comments</comments>
		<pubDate>Thu, 21 May 2009 13:51:46 +0000</pubDate>
		<dc:creator>Frederik Vig</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.frederikvig.com/?p=36</guid>
		<description><![CDATA[Recently I was working on a simple form that used the ASP.NET Validation controls for validation. This worked fine when doing a regular Postback to the server with the data. However I wanted to use Ajax to make it a little faster and the user experience better. One of the problems I had was that [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I was working on a simple form that used the ASP.NET Validation controls for validation. This worked fine when doing a regular Postback to the server with the data. However I wanted to use Ajax to make it a little faster and the user experience better.</p>
<p>One of the problems I had was that when I hooked into my buttons click event with jQuery I needed to make sure the form validated before sending it to my web method. That&#8217;s when I came across this little piece of JavaScript.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>Page_IsValid<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Which does the same thing as Page.IsValid on the server</p>
<p>Below is some simple sample code.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;%</span>@ Page Language<span style="color: #008000;">=</span><span style="color: #666666;">&quot;C#&quot;</span> AutoEventWireup<span style="color: #008000;">=</span><span style="color: #666666;">&quot;true&quot;</span>  CodeFile<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Default.aspx.cs&quot;</span> Inherits<span style="color: #008000;">=</span><span style="color: #666666;">&quot;_Default&quot;</span> <span style="color: #008000;">%&gt;</span>
&nbsp;
<span style="color: #008000;">&lt;!</span>DOCTYPE html <span style="color: #0600FF;">PUBLIC</span> <span style="color: #666666;">&quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;</span> <span style="color: #666666;">&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;</span><span style="color: #008000;">&gt;</span>
&nbsp;
<span style="color: #008000;">&lt;</span>html xmlns<span style="color: #008000;">=</span><span style="color: #666666;">&quot;http://www.w3.org/1999/xhtml&quot;</span><span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;</span>head runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span><span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;</span>title<span style="color: #008000;">&gt;&lt;/</span>title<span style="color: #008000;">&gt;</span>
&nbsp;
    <span style="color: #008000;">&lt;</span>script src<span style="color: #008000;">=</span><span style="color: #666666;">&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js&quot;</span> type<span style="color: #008000;">=</span><span style="color: #666666;">&quot;text/javascript&quot;</span><span style="color: #008000;">&gt;&lt;/</span>script<span style="color: #008000;">&gt;</span>
&nbsp;
    <span style="color: #008000;">&lt;</span>style type<span style="color: #008000;">=</span><span style="color: #666666;">&quot;text/css&quot;</span><span style="color: #008000;">&gt;</span>
        fieldset em <span style="color: #000000;">&#123;</span> color<span style="color: #008000;">:</span> Red<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #008000;">&lt;/</span>style<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;/</span>head<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;</span>body<span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;</span>form id<span style="color: #008000;">=</span><span style="color: #666666;">&quot;form1&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span><span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;</span>fieldset<span style="color: #008000;">&gt;</span>
        <span style="color: #008000;">&lt;</span>p<span style="color: #008000;">&gt;</span>
            <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>Label runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> AssociatedControlID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;txtName&quot;</span><span style="color: #008000;">&gt;</span>Name<span style="color: #008000;">:</span> <span style="color: #008000;">&lt;</span>em title<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Required&quot;</span><span style="color: #008000;">&gt;*&lt;/</span>em<span style="color: #008000;">&gt;&lt;/</span>asp<span style="color: #008000;">:</span>Label<span style="color: #008000;">&gt;</span>
            <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>TextBox runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;txtName&quot;</span> ValidationGroup<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Form&quot;</span> <span style="color: #008000;">/&gt;</span>
            <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>RequiredFieldValidator runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> ControlToValidate<span style="color: #008000;">=</span><span style="color: #666666;">&quot;txtName&quot;</span> Text<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Name is missing&quot;</span> ValidationGroup<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Form&quot;</span> <span style="color: #008000;">/&gt;</span>
        <span style="color: #008000;">&lt;/</span>p<span style="color: #008000;">&gt;</span>
        <span style="color: #008000;">&lt;</span>p<span style="color: #008000;">&gt;</span>
            <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>Button runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;btnSend&quot;</span> Text<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Submit&quot;</span> ValidationGroup<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Form&quot;</span> OnClick<span style="color: #008000;">=</span><span style="color: #666666;">&quot;btnSend_Click&quot;</span> <span style="color: #008000;">/&gt;</span>
        <span style="color: #008000;">&lt;/</span>p<span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;/</span>fieldset<span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;</span>p id<span style="color: #008000;">=</span><span style="color: #666666;">&quot;result&quot;</span><span style="color: #008000;">&gt;</span>
            <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>Literal runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;ltlResult&quot;</span> <span style="color: #008000;">/&gt;</span>
        <span style="color: #008000;">&lt;/</span>p<span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;/</span>form<span style="color: #008000;">&gt;</span>
&nbsp;
    <span style="color: #008000;">&lt;</span>script type<span style="color: #008000;">=</span><span style="color: #666666;">&quot;text/javascript&quot;</span><span style="color: #008000;">&gt;</span>
        $<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;#&lt;%= btnSend.ClientID %&gt;&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">click</span><span style="color: #000000;">&#40;</span>function<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>Page_IsValid<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                var name <span style="color: #008000;">=</span> $<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;#&lt;%= txtName.ClientID %&gt;&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">val</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                $.<span style="color: #0000FF;">ajax</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#123;</span>
                    type<span style="color: #008000;">:</span> <span style="color: #666666;">&quot;POST&quot;</span>,
                    url<span style="color: #008000;">:</span> <span style="color: #666666;">&quot;Default.aspx/YourName&quot;</span>,
                    data<span style="color: #008000;">:</span> <span style="color: #666666;">&quot;{name:'&quot;</span> <span style="color: #008000;">+</span> name <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;'}&quot;</span>,
                    contentType<span style="color: #008000;">:</span> <span style="color: #666666;">&quot;application/json; charset=utf-8&quot;</span>,
                    dataType<span style="color: #008000;">:</span> <span style="color: #666666;">&quot;json&quot;</span>,
                    success<span style="color: #008000;">:</span> function<span style="color: #000000;">&#40;</span>msg<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                        $<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;#result&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">text</span><span style="color: #000000;">&#40;</span>msg.<span style="color: #0000FF;">d</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                    <span style="color: #000000;">&#125;</span>
                <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #0600FF;">return</span> false<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&lt;/</span>script<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;/</span>body<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;/</span>html<span style="color: #008000;">&gt;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Web</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Web.Services</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Web.UI</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">partial</span> <span style="color: #FF0000;">class</span> _Default <span style="color: #008000;">:</span> Page 
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">void</span> btnSend_Click<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, EventArgs e<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>Page.<span style="color: #0000FF;">IsValid</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            ltlResult.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Your name is: {0}&quot;</span>, HttpUtility.<span style="color: #0000FF;">HtmlEncode</span><span style="color: #000000;">&#40;</span>txtName.<span style="color: #0000FF;">Text</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #008080; font-style: italic;">// Make sure to add the [WebMethod] attribute and make the method static</span>
    <span style="color: #000000;">&#91;</span>WebMethod<span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> YourName<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> name<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Your name is: {0}&quot;</span>, HttpUtility.<span style="color: #0000FF;">HtmlEncode</span><span style="color: #000000;">&#40;</span>name<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The reason I still have the btnSend_Click method is in case the user does not have JavaScript enabled.</p>
<p><strong>Related Posts:</strong>
<ul class="similar-posts">
<li><a href="http://www.frederikvig.com/2009/06/using-multiple-forms-on-an-asp-net-web-forms-page/" rel="bookmark" title="June 21, 2009">Using multiple forms on an ASP.NET web forms page</a></li>
<li><a href="http://www.frederikvig.com/2010/05/creating-a-contact-form-with-asp-net-mvc/" rel="bookmark" title="May 13, 2010">Creating a contact form with ASP.NET MVC</a></li>
<li><a href="http://www.frederikvig.com/2009/10/creating-an-episerver-plugin/" rel="bookmark" title="October 8, 2009">Creating an EPiServer Plugin</a></li>
<li><a href="http://www.frederikvig.com/2009/09/extending-search-field-with-suggestion-box/" rel="bookmark" title="September 17, 2009">Extending search field with suggestion box</a></li>
<li><a href="http://www.frederikvig.com/2009/10/font-resizing-and-printing-with-jquery/" rel="bookmark" title="October 27, 2009">Font resizing and printing with jQuery</a></li>
</ul>
<p><!-- Similar Posts took 13.799 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.frederikvig.com/2009/05/aspnet-validation-and-jquery/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Measuring JavaScript performance</title>
		<link>http://www.frederikvig.com/2009/05/measuring-javascript-performance/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=measuring-javascript-performance</link>
		<comments>http://www.frederikvig.com/2009/05/measuring-javascript-performance/#comments</comments>
		<pubDate>Sun, 10 May 2009 14:50:18 +0000</pubDate>
		<dc:creator>Frederik Vig</dc:creator>
				<category><![CDATA[Code Snippet]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://www.frederikvig.com/?p=25</guid>
		<description><![CDATA[According to Response Time overview, the response time for JavaScript code to execeute should be no more than 0.1 seconds (100 milliseconds). Especially on JavaScript/Ajax heavy sites this can be a problem. That&#8217;s why it is always good thing to check the performance of your code. To measure your JavaScript you can use the code [...]]]></description>
			<content:encoded><![CDATA[<p>According to <a href="http://www.useit.com/papers/responsetime.html">Response Time overview</a>, the response time for JavaScript code to execeute should be no more than 0.1 seconds (100 milliseconds). Especially on JavaScript/Ajax heavy sites this can be a problem. That&#8217;s why it is always good thing to check the performance of your code.<br />
To measure your JavaScript you can use the code below:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> somefunction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> start <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getMilliseconds</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">// code here</span>
    <span style="color: #003366; font-weight: bold;">var</span> <span style="color: #000066;">stop</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getMilliseconds</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> executionTime <span style="color: #339933;">=</span> <span style="color: #000066;">stop</span> <span style="color: #339933;">-</span> start<span style="color: #339933;">;</span>
    console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Execution time &quot;</span> <span style="color: #339933;">+</span> executionTime <span style="color: #339933;">+</span>
        <span style="color: #3366CC;">&quot; milliseconds&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// or alert(&quot;Execution time &quot; + executionTime + &quot; milliseconds&quot;);</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Note that <a href="http://www.getfirebug.com">Firebug</a> for <a href="http://www.getfirefox.com">Firefox</a> also has a code profiler which does the same thing. To use it install Firebug, enable firebug (f12), activate the console window, press the profile button, refresh the page, click the profile button again (more information: <a href="http://getfirebug.com/js.html">Firebug JavaScript Debugger and Profiler</a>).<br />
<a href="http://www.frederikvig.com/wp-content/uploads/firebug-profiler.png"><img class="alignnone size-full wp-image-27" title="Firebug profiler" src="http://www.frederikvig.com/wp-content/uploads/firebug-profiler.png" alt="Firebug profiler" width="795" height="647" /></a><strong>Related Posts:</strong>
<ul class="similar-posts">
<li><a href="http://www.frederikvig.com/2009/12/part-8-preparing-for-launch-create-an-episerver-site-from-scratch/" rel="bookmark" title="December 24, 2009">Part 8: Preparing for launch – Create an EPiServer site from scratch</a></li>
<li><a href="http://www.frederikvig.com/2010/05/creating-a-contact-form-with-asp-net-mvc/" rel="bookmark" title="May 13, 2010">Creating a contact form with ASP.NET MVC</a></li>
<li><a href="http://www.frederikvig.com/2009/06/using-multiple-forms-on-an-asp-net-web-forms-page/" rel="bookmark" title="June 21, 2009">Using multiple forms on an ASP.NET web forms page</a></li>
<li><a href="http://www.frederikvig.com/2010/04/detecting-ajax-requests-on-the-server/" rel="bookmark" title="April 6, 2010">Detecting Ajax requests on the server</a></li>
<li><a href="http://www.frederikvig.com/2009/10/font-resizing-and-printing-with-jquery/" rel="bookmark" title="October 27, 2009">Font resizing and printing with jQuery</a></li>
</ul>
<p><!-- Similar Posts took 11.962 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.frederikvig.com/2009/05/measuring-javascript-performance/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->