<?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/category/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>My First Plugin</title>
		<link>http://www.djsipe.com/2008/01/31/my-first-plugin/</link>
		<comments>http://www.djsipe.com/2008/01/31/my-first-plugin/#comments</comments>
		<pubDate>Fri, 01 Feb 2008 03:07:32 +0000</pubDate>
		<dc:creator>DJ</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Plugin]]></category>
		<category><![CDATA[code face]]></category>
		<category><![CDATA[progressive enhancement]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[yui]]></category>

		<guid isPermaLink="false">http://www.djsipe.com/2008/01/31/my-first-plugin/</guid>
		<description><![CDATA[After a few posts where I wanted to include code examples, I quickly became frustrated with the standard &#60;pre&#62; display that the folks at WordPress recommend. I looked for a plugin that worked well, but didn&#8217;t like what I was seeing. So at the prodding of my colleague, Pat Doran, I decided to roll my [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.djsipe.com/code-face-plugin"><img src="http://wordpress.plugin.codeface.s3.amazonaws.com/code-face.gif" style="margin: 0pt 10px 10px 0pt" alt="Code Face" align="left" height="145" width="263" /></a>After a few posts where I wanted to include code examples, I quickly became frustrated with the standard &lt;pre&gt; display that the folks at WordPress <a href="http://codex.wordpress.org/Writing_Code_in_Your_Posts" target="_blank">recommend</a>.  I looked for a plugin that worked well, but didn&#8217;t like what I was seeing.  So at the prodding of my colleague, <a href="http://rockland-ave.com/blog/" target="_blank">Pat Doran</a>, I decided to roll my own plugin.</p>
<p>The result is my &#8220;<a href="http://www.djsipe.com/code-face-plugin">Code Face</a>&#8221; plugin.  It uses the <a href="http://developer.yahoo.com/yui/" target="_blank">YUI</a> JavaScript framework to scan your posts and replace specially tagged &lt;pre&gt; blocks with rich code syntax highlighting via the <a href="http://code.google.com/p/syntaxhighlighter/" target="_blank">syntaxhighlighter</a> utility. Ninety-nine percent of the plugin is written in JavaScript using the concept of <a href="http://en.wikipedia.org/wiki/Progressive_enhancement" target="_blank">progressive enhancement</a>.  That means that even if your readers have JavaScript turned off or are using a screen reader, the content of your post is still highly accessible.</p>
<p><a href="http://www.djsipe.com/code-face-plugin">Check it out</a> and let me know what you think.  I&#8217;d love to hear your comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.djsipe.com/2008/01/31/my-first-plugin/feed/</wfw:commentRss>
		<slash:comments>3</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>
	</channel>
</rss>
