<?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>djsipe.com &#187; JavaScript</title>
	<atom:link href="http://www.djsipe.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.djsipe.com</link>
	<description>Web Development</description>
	<lastBuildDate>Tue, 15 Sep 2009 01:14:53 +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>Doing Pop-Ups the Right Way with jQuery</title>
		<link>http://www.djsipe.com/2008/07/04/doing-pop-ups-the-right-way-with-jquery/</link>
		<comments>http://www.djsipe.com/2008/07/04/doing-pop-ups-the-right-way-with-jquery/#comments</comments>
		<pubDate>Fri, 04 Jul 2008 18:57:17 +0000</pubDate>
		<dc:creator>DJ</dc:creator>
				<category><![CDATA[Front End]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[popup]]></category>

		<guid isPermaLink="false">http://www.djsipe.com/2008/07/04/doing-pop-ups-the-right-way-with-jquery/</guid>
		<description><![CDATA[We&#8217;ve all seen them, links that look like they go somewhere but when you roll your mouse over them you see something like &#8220;javascript:popupWindow();&#8221; in the status bar instead of a URL. Putting JavaScript into the href of links is just a horrible idea as it severely degrades the accessibility of your site (and ticks [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;ve all seen them, links that look like they go somewhere but when you roll your mouse over them you see something like &#8220;javascript:popupWindow();&#8221; in the status bar instead of a URL.  Putting JavaScript into the href of links is just a horrible idea as it severely degrades the accessibility of your site (and ticks off people like me who want to see where they&#8217;re about to be taken before they click).  But rather than just complain about it, I thought I&#8217;d put together some code to help us all avoid this situation all together.</p>
<p>The ideal solution is one that will work for the greatest number of users.  Given that, we need a solution that will use <a href="http://en.wikipedia.org/wiki/Unobtrusive_JavaScript" target="_blank">unobtrusive JavaScript</a> to enhance a standard HTML page with customizable popup windows&#8212;rather than rely solely on JavaScript.  Since straight-up HTML supports opening links in new windows by <a href="http://wp.netscape.com/eng/mozilla/2.0/relnotes/demo/target.html" target="_blank">adding a target attribute to the link</a>, we start off with a link that looks like this:</p>
<pre class="html-codeface">&lt;a href="/popup.html" target="popupWindow" class="popup"&gt;Open new window&lt;/a&gt;</pre>
<p>Using this as a starting point, the next step is to create a simple popup window function that wraps the standard DOM method: <a href="http://developer.mozilla.org/en/docs/DOM:window.open" target="_blank">window.open()</a>.  I use this function to do a few things:</p>
<ol>
<li>Enforce default parameters rather than depend on the browser&#8217;s defaults</li>
<li>Provide defaults that favor greater accessibility, ie: scrollbars and resize</li>
<li>Allow a <a href="http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Literals#Object_Literals" target="_blank">JavaScript object literal</a> to be used instead of the standard configuration string</li>
</ol>
<p>Here&#8217;s the function:</p>
<pre class="js-codeface">// Create a namespace for our utilities
var UTIL = UTIL || {};
UTIL.popup = UTIL.popup || {};

/**
 * Open popup window
 *
 * Opens a popup window using as little as a URL. An optional params object can
 * be passed.
 *
 * @param {String} href
 * @param {Object} params
 * @return {WindowObjectReference}
 */
UTIL.popup.open = function (href, params)
{
    // Defaults (don't leave it to the browser)
    var defaultParams = {
        "width":       "800",   // Window width
        "height":      "600",   // Window height
        "top":         "0",     // Y offset (in pixels) from top of screen
        "left":        "0",     // X offset (in pixels) from left side of screen
        "directories": "no",    // Show directories/Links bar?
        "location":    "no",    // Show location/address bar?
        "resizeable":  "yes",   // Make the window resizable?
        "menubar":     "no",    // Show the menu bar?
        "toolbar":     "no",    // Show the tool (Back button etc.) bar?
        "scrollbars":  "yes",   // Show scrollbars?
        "status":      "no"     // Show the status bar?
    };

    var windowName = params["windowName"] || "new_window";

    var i, useParams = "";

    // Override defaults with custom values while we construct the params string
    for (i in defaultParams)
    {
        useParams += (useParams === "") ? "" : ",";
        useParams += i + "=";
        useParams += params[i] || defaultParams[i];
    }

    return window.open(href, windowName, useParams);
};</pre>
<p>Admittedly, this part isn&#8217;t rocket science and could probably be done in a more elegant way, but it&#8217;s needed to really open things up for the next part.  Now we get to the fun stuff.  Using <a href="http://jquery.com/" title="jQuery" target="_blank">jQuery</a>, we search the document for all links that have a CSS class of &#8220;popup&#8221;.  For each one we find, we add an <a href="http://developer.mozilla.org/en/docs/onclick" target="_blank">onClick handler</a> that disables the browser&#8217;s default onClick behavior for links and then opens up a popup window using the links href attribute.  Here&#8217;s the code:</p>
<pre class="js-codeface">$(function(){ // Run this code when the document's done loading    

    // Apply this code to each link with class="popup"
    $("a.popup").each(function (i){

        // Add an onClick behavior to this link
        $(this).click(function(event) {

            // Prevent the browser's default onClick handler
            event.preventDefault();

            // Grab parameters using jQuery's data() method
            var params = $(this).data("popup") || {};            

            // Use the target attribute as the window name
            if ($(this).attr("target"))
            {
                params.windowName = $(this).attr("target");
            }

            // Pop up the window
            var windowObject = UTIL.popup.open(this.href, params);

            // Save the window object for other code to use
            $(this).data("windowObject", windowObject);
        });
    });
});</pre>
<p>One of the great features of jQuery that we utilize here is the <a href="http://docs.jquery.com/Core/data" title="jQuery's data() method" target="_blank">data method</a>.  This allows us to attach data to DOM elements without corrupting the HTML with non-standard attributes or tags.  Using jQuery&#8217;s ability to locate DOM elements using CSS selectors, we can bind the custom configuration object (used in our new popup function) to the links themselves.  Then, when a link is clicked, it can read it&#8217;s popup configuration and pass it to the popup window function.  In this way, we can keep our HTML standards compliant <em>and </em>completely separate from our JavaScript code.</p>
<p>Putting it all together, we can create an HTML page like this:</p>
<pre class="html-codeface">&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html&gt;
   &lt;head&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /&gt;
      &lt;title&gt;Popup Window Test&lt;/title&gt;
      &lt;script type="text/javascript" src="jquery-1.2.6.pack.js"&gt;&lt;/script&gt;
      &lt;script type="text/javascript" src="utils.js"&gt;&lt;/script&gt;
   &lt;/head&gt;
   &lt;body&gt;
      &lt;ul&gt;
         &lt;li&gt;
            This link uses defaults:
            &lt;a href="http://google.com" target="google" class="popup"&gt;Google.com&lt;/a&gt;
         &lt;/li&gt;
         &lt;li&gt;
            This link uses custom parameters:
            &lt;a id="custom-popup" href="http://yahoo.com" target="yahoo" class="popup"&gt;Yahoo.com&lt;/a&gt;
         &lt;/li&gt;
      &lt;/ul&gt;
      &lt;script type="text/javascript"&gt;

         // Add custom pop-up properties to the second link
         $("a#custom-popup").data("popup", {width:400,height:400, top:200, left:200});

      &lt;/script&gt;
   &lt;/body&gt;
&lt;/html&gt;</pre>
<p>If you would like to download this example, you can get it <a href="https://com.djsipe.s3.amazonaws.com/demos/popup-test.zip">here</a> (jQuery not included for legal reasons).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.djsipe.com/2008/07/04/doing-pop-ups-the-right-way-with-jquery/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Click to Activate and Use This Control</title>
		<link>http://www.djsipe.com/2008/01/20/click-to-activate-and-use-this-control/</link>
		<comments>http://www.djsipe.com/2008/01/20/click-to-activate-and-use-this-control/#comments</comments>
		<pubDate>Mon, 21 Jan 2008 01:33:00 +0000</pubDate>
		<dc:creator>DJ</dc:creator>
				<category><![CDATA[Front End]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[ie]]></category>

		<guid isPermaLink="false">http://www.djsipe.com/2008/01/20/click-to-activate-and-use-this-control/</guid>
		<description><![CDATA[Ever get that annoying &#8220;Click to activate and use this control&#8221; message in IE when you embed a Flash movie? I did and couldn&#8217;t find a solution that worked as well as I&#8217;d like. A quick browse of my Google results brought be here, but the solution there was kinda sloppy in my opinion. But [...]]]></description>
			<content:encoded><![CDATA[<p>Ever get that annoying &#8220;Click to activate and use this control&#8221; message in IE when you embed a Flash movie?  I did and couldn&#8217;t find a solution that worked as well as I&#8217;d like.  A quick browse of my Google results brought be <a href="http://www.mix-fx.com/flash-prompt.htm" target="_blank">here</a>, but the solution there was kinda sloppy in my opinion.  But with a few tweaks, I got things working&#8230;and with the added street cred&#8217; of not introducing a single global variable.</p>
<p>Here&#8217;s the code:</p>
<pre class="js-codeface">
(function()
{
    var clear = function(a)
    {
        for(var i=0; i &lt; a.length; i++)
        {
            a[i].outerHTML=a[i].outerHTML;
        }
    };

    clear(document.getElementsByTagName("object"));
    clear(document.getElementsByTagName("embed"));
})();</pre>
<p>The original code I found only applied the fix to <code>object</code> tags.  But since use of the <code>object</code> tag is becoming deprecated, we also need to touch the <code>embed</code> tags as well.  The code above does both and is wrapped in an anonymous function to keep all is private bits private.</p>
<p>Finally, since this is an IE-only fix, we should make use of IE&#8217;s <a href="http://www.quirksmode.org/css/condcom.html" target="_blank">conditional comments</a> so other browsers don&#8217;t have to be bothered by the script.  Assuming we placed (a <a href="http://javascript.crockford.com/jsmin.html" target="_blank">minified</a> version of) the code into a file named <code>controlFix.js</code>, we then add the following code at the end of the source code:</p>
<pre class="html-codeface">&lt;!--[if IE]&gt;
&lt;script type="text/javascript" src="controlFix.js"&gt;&lt;/script&gt;
&lt;![endif]--&gt;</pre>
<p>If you&#8217;d like to use this on your site, you can download the minified JavaScript <a href="https://djsipe.code.s3.amazonaws.com/controlFix.zip">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.djsipe.com/2008/01/20/click-to-activate-and-use-this-control/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Boosting Front-End Load Time with PHP</title>
		<link>http://www.djsipe.com/2008/01/20/boosting-front-end-load-time-with-php/</link>
		<comments>http://www.djsipe.com/2008/01/20/boosting-front-end-load-time-with-php/#comments</comments>
		<pubDate>Sun, 20 Jan 2008 17:13:42 +0000</pubDate>
		<dc:creator>DJ</dc:creator>
				<category><![CDATA[Front End]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.djsipe.com/2008/01/20/boosting-front-end-load-time-with-php/</guid>
		<description><![CDATA[On of the best tools around for helping you speed up your page load times is Yahoo!&#8217;s YSlow Firefox extension. I&#8217;ve been using it for a while and found it has some interesting insights on page load performance. Its recommendations come from Yahoo!&#8217;s &#8220;Exceptional Performance Team&#8221; 14 recommendations for speeding up page load times. If [...]]]></description>
			<content:encoded><![CDATA[<p>On of the best tools around for helping you speed up your page load times is Yahoo!&#8217;s <a href="http://developer.yahoo.com/yslow/" title="YSlow explained on Yahoo!'s Developer Network" target="_blank">YSlow</a> Firefox extension.  I&#8217;ve been using it for a while and found it has some interesting insights on page load performance.  Its recommendations come from Yahoo!&#8217;s &#8220;Exceptional Performance Team&#8221; 14 recommendations for speeding up page load times.  If you&#8217;d rather hear it right from <a href="http://video.yahoo.com/video/play?vid=1040890" target="_blank">the horses mouth</a>, have at, otherwise you can watch the conveniently embedded video below.</p>
<p><embed src="http://us.i1.yimg.com/cosmos.bcst.yahoo.com/player/media/swf/FLVVideoSolo.swf" style="margin: 0pt 10px 10px 0pt; float: left" flashvars="id=3880720&amp;emailUrl=http%3A%2F%2Fvideo.yahoo.com%2Futil%2Fmail%3Fei%3DUTF-8%26vid%3D1040890&amp;imUrl=http%253A%252F%252Fvideo.yahoo.com%252Fvideo%252Fplay%253Fei%253DUTF-8%2526vid%253D1040890&amp;imTitle=Steve%2BSouders%253A%2B%2526quot%253BHigh%2BPerformance%2BWeb%2BSites%253A%2B14%2BRules%2Bfor%2BFaster%2BPages%2526quot%253B&amp;searchUrl=http://video.yahoo.com/search/video?p=&amp;profileUrl=http://video.yahoo.com/video/profile?yid=&amp;creatorValue=ZXJpY21pcmFnbGlh&amp;vid=1040890" type="application/x-shockwave-flash" height="350" width="425"></embed>Briefly, the 14 recommendations are:</p>
<ol>
<li>Make fewer HTTP requests</li>
<li>Use a <a href="http://en.wikipedia.org/wiki/Content_Delivery_Network" target="_blank">CDN</a> (like Akamai)</li>
<li>Add an &#8220;Expires&#8221; header</li>
<li>Use GZIP compression</li>
<li>Put CSS at the top</li>
<li>Put JavaScript (JS) at the bottom</li>
<li>Avoid CSS expressions</li>
<li>Make JS and CSS external</li>
<li>Reduce DNS lookups</li>
<li>Minify JS</li>
<li>Avoid redirects</li>
<li>Remove duplicate scripts</li>
<li>Configure ETags</li>
<li>Make AJAX cacheable</li>
</ol>
<p>Since the video can probably explain each of these points better than I can, I&#8217;ll let them speak for themselves for the most part.  Lately though, I have been thinking a lot about two of them, namely #1 and #10: Make fewer HTTP requests and Minify JavaScript.  Bit by bit, I&#8217;ve been trying to get all our JavaScript files at work &#8220;minified&#8221; but, it&#8217;s an uphill battle with so many files being edited semi-frequently by people other than myself.  Since minified JavaScript is almost impossible to read without getting a headache, you&#8217;d have to keep the un-minified versions on tap somewhere and leave step by step instructions on how to minify them and then get them on the site.  Having people then copy and paste the minified versions into a single aggregate file is simply asking for trouble.</p>
<p>Then I found a key part of the solution: a port of <a href="http://javascript.crockford.com/jsmin.html" target="_blank">Crockford&#8217;s JSMin</a> to PHP.  This <a href="http://code.google.com/p/jsmin-php/" target="_blank">PHP version</a>, allows you to do something very powerful that the original JavaScript version did not.  Now you can use aggregate and minify all your JavaScript into a single HTTP request by calling a PHP script in your script tag which gathers and minifies all the JavaScript for the page.</p>
<p>So, say you wanted to aggregate and minify the JavaScript for your menu and rich text editor of choice.  In your HTML, you might use a script tag like this:</p>
<pre class="html-codeface">&lt;script type="text/javascript" src="/jsaggmin.php?features=menu/richtexteditor"&gt;&lt;/script&gt;</pre>
<p>On the server, <code>jsaggmin.php</code> would do look something like this:</p>
<pre class="php-codeface">
require "jsmin.php";

foreach (explode("/", $_GET['features']) as $feature)
{

    switch ($feature)
    {
        case "menu":
            echo JSMin::minify(file_get_contents("menu.js"));
            break;

        case "richtexteditor":
            echo JSMin::minify(file_get_contents("rte.js"));
            break;
    }

}</pre>
<p>Granted, this isn&#8217;t the complete solution, but it&#8217;s a leap in the right direction.  What is needed next (in addition to a caching mechanism) is a way to run the equivalent of <a href="http://www.jslint.com/" target="_blank">JSLint</a> on the server before the files are minified.  As a rule of thumb, if a JavaScript file can&#8217;t pass JSLint, it will blow up when minified.  So the PHP script would need to verify the JavaScript, and echo it as is if it fails to validate.  Otherwise, you&#8217;ve just minified and aggregated all the scripts on your page and can focus on the other 12 recommendations.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.djsipe.com/2008/01/20/boosting-front-end-load-time-with-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
