<?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>TechBubble &#187; Programming</title>
	<atom:link href="http://www.kalyani.com/tag/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kalyani.com</link>
	<description>Nik Kalyani&#039;s Irrationally Exuberant Musings on Technology</description>
	<lastBuildDate>Mon, 23 Jan 2012 08:16:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>XHTML-Compliant Technique for Passing Parameters to Javascript Script Files</title>
		<link>http://www.kalyani.com/2008/08/xhtml-compliant-technique-for-passing-parameters-to-javascript-script-files/</link>
		<comments>http://www.kalyani.com/2008/08/xhtml-compliant-technique-for-passing-parameters-to-javascript-script-files/#comments</comments>
		<pubDate>Fri, 22 Aug 2008 08:00:00 +0000</pubDate>
		<dc:creator>nik</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.techbubble.net/Blog/tabid/57/EntryId/211/XHTML-Compliant-Technique-for-Passing-Parameters-to-Javascript-Script-Files.aspx</guid>
		<description><![CDATA[Joe Brinkman posted a technique to pass parameters to Javascript script files. The approach is simple &#8212; append a standard querystring to the script URL and obtain the parameters by locating the script element using the ID attribute, and parsing the key-value pairs from the &#8220;src&#8221; attribute. A sample usage is like this: &#38;lt;script id=&#38;quot;MyScript&#38;quot; <a href='http://www.kalyani.com/2008/08/xhtml-compliant-technique-for-passing-parameters-to-javascript-script-files/'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p>Joe Brinkman <a href="http://blog.theaccidentalgeek.com/post/2008/08/21/Passing-parameters-to-JavaScript-files.aspx">posted</a> a technique to pass parameters to <cite>Javascript</cite> script files. The approach is simple &#8212; append a standard querystring to the script URL and obtain the parameters by locating the script element using the ID attribute, and parsing the key-value pairs from the &#8220;src&#8221; attribute. A sample usage is like this:</p>
<pre class="brush: html">
&amp;lt;script id=&amp;quot;MyScript&amp;quot; src=&amp;quot;http://www.foo.com/myscript.js?obj1=ABC&amp;amp;amp;obj2=XYZ&amp;quot; type=&amp;quot;text/javascript&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;
</pre>
<p>This is a great approach and works well for most scenarios, except in those situations where HTML 4.01 Strict markup is desired. In this case there are two problems that would cause validation to fail:</p>
<p>1) The <cite>HTML</cite> &#8220;script&#8221; element does not support an ID attribute. Yes, this is true. Verify it for yourself <a href="http://www.w3.org/TR/html401/interact/scripts.html#edef-SCRIPT">here</a>.</p>
<p>2) The ampersand (&#8220;&amp;&#8221;) character is a predefined entity and <a href="http://www.w3.org/TR/xhtml1/#C_12">must be expressed as an entity reference</a> &#8220;&amp;&#8221;</p>
<p>So, is there a simple way to work around this issue. Of course, otherwise the title of this post would be totally false advertising.</p>
<p>Here is the revised code snippet I propose in lieu of the one above:</p>
<pre class="brush: html">&amp;lt;script src=&amp;quot;http://www.foo.com/myscript.js#obj1/ABC/obj2/XYZ&amp;quot; type=&amp;quot;text/javascript&amp;quot;&amp;gt;</pre>
<p>Let&#8217;s break it down.</p>
<p>1) <strong>The ID attribute has been removed.</strong> Even without the ID attribute there is still a simple way to reference the script element. The trick is to take advantage of the fact that as the browser is rendering the page, it executes JS code immediately when encountered. As a result, if the code in your JS file queries the DOM, the last script element in the DOM is a self-reference (i.e. a reference to the script element that loaded the code being executed). Thus, using <em>document.getElementsByTagName(&#8220;script&#8221;)</em> and referencing the last element in the array, provides an easy way to get the current script reference.</p>
<p>2) <strong>The standard querystring separator &#8220;?&#8221; has been replaced by &#8220;#&#8221;.</strong> This is not necessary, but something I did for semantic reasons. To overcome the &#8220;&amp;&#8221; entity issue, I used a slash (&#8220;/&#8221;) character for separating not only key-value pairs, but keys and values too. The reason for this will become evident in a bit, but since I modified the querystring to something non-standard, I felt it was important to also remove the &#8220;?&#8221; separator which is a prefix for querystring. Instead, I used a hash (&#8220;#&#8221;) character which is a pointer to a document fragment and does not have any semantic value in the context of a JS script URL. (In fact the JS URL hash technique is a common way to pass data in cross-domain XMLHttp requests for this reason and also because it does not cause a page re-load.)</p>
<p>3) <strong>Key-Value pairs. </strong>The last change I made is to use a path-based approach to key-value pairs instead of Key=Value format. I did this because it is a common technique in RESTful URL&#8217;s, is easier to read and much simpler to parse. In fact, using a single &#8220;for&#8230;&#8221; loop, it becomes a trivial task to create an associative array of all the parameters for ready reference.</p>
<p>Here&#8217;s the code wrapped into a function with a sample call:</p>
<pre class="brush: javascript">

var myParams = getScriptUrlParams();
alert(myParams[&amp;quot;obj1&amp;quot;]);
alert(myParams[&amp;quot;obj2&amp;quot;]);

function getScriptUrlParams()
{
	var scriptTags = document.getElementsByTagName(&amp;quot;script&amp;quot;);

	// This code is assumed to be in a file so the &amp;quot;src&amp;quot; attribute
	// is guaranteed to be present...no error-checking is needed
	var urlFrags = scriptTags[scriptTags.length-1].src.split(&amp;quot;#&amp;quot;);

	var urlParams=[];
	var urlParamRaw = [];
	if (urlFrags.length &amp;gt; 1)
	{
	    urlParamRaw = urlFrags[1].split(&amp;quot;/&amp;quot;);
	    if (urlParamRaw.length &amp;gt;= 2)
	    {
	    	for(var param=0;param&amp;lt;urlParamRaw.length;param+=2)
	            urlParams[urlParamRaw[param]] = (urlParamRaw.length &amp;gt;= param + 1 ? unescape(urlParamRaw[param+1]) : null);
    	    }
	}

	return(urlParams);
}
</pre>
<p>What do you think? Is this a simpler approach or is it more complicated? Are there other techniques for working around the X<cite>HTML</cite> validation issue.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kalyani.com/2008/08/xhtml-compliant-technique-for-passing-parameters-to-javascript-script-files/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

