<?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>Dusty Reagan &#187; JavaScript</title>
	<atom:link href="http://dustyreagan.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://dustyreagan.com</link>
	<description>On Technology &#38; Entrepreneurship</description>
	<lastBuildDate>Thu, 15 Jul 2010 01:17:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Two Javascript Tricks: click() and trim()</title>
		<link>http://dustyreagan.com/two-javascript-tricks-click-and-trim/</link>
		<comments>http://dustyreagan.com/two-javascript-tricks-click-and-trim/#comments</comments>
		<pubDate>Tue, 28 Nov 2006 00:37:00 +0000</pubDate>
		<dc:creator>Dusty Reagan</dc:creator>
				<category><![CDATA[Web & Software Development]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://dev.dustyreagan.com/?p=23</guid>
		<description><![CDATA[Man my poor blog hasn&#8217;t been updated in quite some time. What on earth could I do to remedy that situation? Wait, I GOT IT! I&#8217;ll add a new post! Here are two really simple but really useful JavaScript tricks. &#8230; <a href="http://dustyreagan.com/two-javascript-tricks-click-and-trim/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Man my poor blog hasn&#8217;t been updated in quite some time. What on earth could I do to remedy that situation? Wait, I GOT IT! I&#8217;ll add a new post! <img src='http://dustyreagan.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Here are two really simple but really useful JavaScript tricks.</p>
<p><strong>#1</strong></p>
<pre>document.getElementById(x).click();</pre>
<p>The above is handy when you want to programmatically click a button for the user based on whatever logic you write. I used it to fire some ATLAS (now called ASP.Net AJAX) animations after I validated some fields.</p>
<p><strong>#2</strong></p>
<p>Add the following to your JavaScript library to trim the white space from the beginning and end of your strings.</p>
<pre>String.prototype.trim=function()
{
	return this.replace(/^\s*|\s*$/g,'');
}</pre>
<p>The above allows you to do this: xString = xString.trim();</p>
<p>Check out this Wikipedia article on &#8216;trim&#8217;. There are examples on how to trim a string for numerous programming languages.</p>
<p><a href="http://en.wikipedia.org/wiki/Trim_(programming)">http://en.wikipedia.org/wiki/Trim_(programming)</a></p>
<img src="http://dustyreagan.com/?ak_action=api_record_view&id=23&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://dustyreagan.com/two-javascript-tricks-click-and-trim/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to submit HTML without disabling ValidateRequest</title>
		<link>http://dustyreagan.com/how-to-submit-html-without-disabling/</link>
		<comments>http://dustyreagan.com/how-to-submit-html-without-disabling/#comments</comments>
		<pubDate>Tue, 11 Jul 2006 03:30:00 +0000</pubDate>
		<dc:creator>Dusty Reagan</dc:creator>
				<category><![CDATA[Web & Software Development]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://dev.dustyreagan.com/?p=20</guid>
		<description><![CDATA[So the scenario is this. You have an ASP.Net form field that you want the user to submit HTML to the server with. By default ASP.Net won&#8217;t allow this for security reasons. You could get around this by setting ValidateRequest=&#8221;false&#8221; &#8230; <a href="http://dustyreagan.com/how-to-submit-html-without-disabling/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>So the scenario is this. You have an ASP.Net form field that you want the user to submit HTML to the server with. By default ASP.Net won&#8217;t allow this for security reasons. You could get around this by setting ValidateRequest=&#8221;false&#8221; in the page declaration or in the web.config. But, if you are developing a user control, you don&#8217;t want to make the developer using your control have to manage this. Another reason may be that subsequent developers may not be aware that you&#8217;ve deliberately opened up a security hole that has to be managed. So, you want to keep ValidateRequest enabled, but how?</p>
<p>To do this I wrote a little &#8216;encodeMyHtml&#8217; JavaScript function that is called on the OnClick event when the HTML form’s submit button is clicked. The function encodes the user&#8217;s HTML input for the field I&#8217;ve specified into a harmless string before it is passed to the server. When I receive that input on the server I simply decode and go on my way.</p>
<p>ValidateRequest is happy, our users are happy, our peers are happy, heck we&#8217;re happy.</p>
<p>I add my &#8216;encodeMyHtml&#8217; JavaScript function in my user control&#8217;s OnPageLoad method. This way I can make sure that my JavaScript is added to the parent page only once, no matter how many controls are on the page.</p>
<p>In my control’s OnPageLoad I call this:</p>
<pre>private void addEditorJavaScript()
{
    // create our HTML encoder javascript function
    // this way it shows up once per page that the control is on
    string scr = @"&lt;script type='text/javascript'&gt;function encodeMyHtml(name){
                var content = document.getElementById(name).value
                content = content.replace(/&lt;/g,'&lt;');
                content = content.replace(/&gt;/g,'&gt;');
                document.getElementById(name).value = content;
            }&lt;/script&gt;";

    // add the javascript into the Page
    ClientScriptManager cm = Page.ClientScript;
    cm.RegisterClientScriptBlock(this.GetType(), "GlobalJavascript", scr);
}</pre>
<p>In my control’s ASPX I’m using a gridview. I wrap the gridview’s update asp:LinkButton in a span tag, and in that span tag I put my OnClickEvent.</p>
<pre>&lt;span onclick="encodeMyHtml('&lt;%# UniqueID.Replace("$", "_") %&gt;_FormViewContentManager_ContentTextBox')"&gt;
    &lt;asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="[Publish]" /&gt;
&lt;/span&gt;&lt;span onclick=&quot;encodeMyHtml(&#039;  </span></pre>
<p>When I get the input on the server side I simply call a couple of Replace methods on the input string to decode the HTML, and I’m done.</p>
<img src="http://dustyreagan.com/?ak_action=api_record_view&id=20&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://dustyreagan.com/how-to-submit-html-without-disabling/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
