<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" version="2.0">

<channel>
	<title>Coding Tips</title>
	
	<link>http://www.codingtips.org</link>
	<description>Quick Tips for Effective Programming!</description>
	<pubDate>Wed, 30 Apr 2008 06:31:14 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/codingtips" type="application/rss+xml" /><item>
		<title>Getting started with Ajax - build your own smallest ajax framework</title>
		<link>http://www.codingtips.org/ajax/getting-started-with-ajax-build-your-own-smallest-ajax-framework.html</link>
		<comments>http://www.codingtips.org/ajax/getting-started-with-ajax-build-your-own-smallest-ajax-framework.html#comments</comments>
		<pubDate>Wed, 30 Apr 2008 06:31:14 +0000</pubDate>
		<dc:creator>codin</dc:creator>
		
		<category><![CDATA[Ajax]]></category>

		<guid isPermaLink="false">http://www.codingtips.org/?p=16</guid>
		<description><![CDATA[Ajax (Asynchronous JavaScript and XML) have drastically changed the way Web application work. Effective and controlled use of Ajax can make Web applications highly responsive. In this article, I will take you through the fundamentals of Ajax. We also build a small ajax library JavaScript file which can be used as the starting point for [...]]]></description>
			<content:encoded><![CDATA[<p>Ajax (Asynchronous JavaScript and XML) have drastically changed the way Web application work. Effective and controlled use of Ajax can make Web applications highly responsive. In this article, I will take you through the fundamentals of Ajax. We also build a small ajax library JavaScript file which can be used as the starting point for your projects.</p>
<p>Note that there are a lot of Ajax frameworks available such as DOJO and YUI. But one issue with these frameworks is that they are somewhat big. If your application has to work with a lot of dialup users, you will have optimize these frameworks. Also you will have to make use of tools such as YUI compressor.</p>
<p>So if you are just looking for simple Ajax functionalities such as refreshing a portion of a Web page, it is better to go with your own small Ajax framework. </p>
<p>Ajax as the term suggests enables us to retrieve content asynchronously over HTTP request. Along with DHTML you can use this to update selected sections of a Web page.</p>
<p>In order to use Ajax, you need to first get a JavaScript browser object called XMLHttpRequest. Using this you can make asynchronous HTTP requests. But the implementation of this object is unfortunately browser dependent! So our first task is to write a JavaScript function which returns XMLHttpRequest in a browser independent way. Let us call this function getXMLHttpRequest(),</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> getXMLHttpRequest<span style="color: #009900;">&#40;</span><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>window.<span style="color: #660066;">XMLHttpRequest</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
         <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">new</span> XMLHttpRequest<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: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">ActiveXObject</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
         <span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">new</span> ActiveXObject<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Msxml2.XMLHTTP&quot;</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;">catch</span> <span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
               <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">new</span> ActiveXObject<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Microsoft.XMLHTTP&quot;</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;">catch</span> <span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
         <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
      <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>

<p>Now let us write a function which will use the XMLHttpRequest to do an HTTP GET request,</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> getAjaxContent<span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span> callback<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> ajax_req <span style="color: #339933;">=</span> getXMLHttpRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	ajax_req.<span style="color: #660066;">onreadystatechange</span> <span style="color: #339933;">=</span> callback<span style="color: #339933;">;</span>
	ajax_req.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'GET'</span><span style="color: #339933;">,</span>url<span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        ajax_req.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>But where do we get the content of our Ajax request? The second argument to the getAjaxContent is a function pointer and this function will be invoked during Ajax execution. Remember - Ajax execution is asynchronous!</p>
<p>Now in our application we can use the above two methods for Ajax calls. Here is a sample which retrieves an RFC document using Ajax,</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> getRFC22<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   getAjaxContent<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://www.ietf.org/rfc/rfc0022.txt&quot;</span><span style="color: #339933;">,</span>cb<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> cb<span style="color: #009900;">&#40;</span><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><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">readyState</span><span style="color: #339933;">==</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">responseText</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>If the URL you are accessing returns HTML, you will need to use this.responseXML instead of this.responseText</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingtips.org/ajax/getting-started-with-ajax-build-your-own-smallest-ajax-framework.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>Implementing pagination at database level (Oracle SQL) - Use RANK</title>
		<link>http://www.codingtips.org/sql/implementing-pagination-at-database-level-oracle-sql-use-rank.html</link>
		<comments>http://www.codingtips.org/sql/implementing-pagination-at-database-level-oracle-sql-use-rank.html#comments</comments>
		<pubDate>Tue, 29 Apr 2008 06:06:12 +0000</pubDate>
		<dc:creator>codin</dc:creator>
		
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.codingtips.org/?p=15</guid>
		<description><![CDATA[It is a common requirement to have pagination in Web applications. The easy way is to query all the records and then apply pagination logic to the data in memory. But this approach won&#8217;t work with tables which contains huge data sets.
The best approach is to do the pagination at database level using SQL. Database [...]]]></description>
			<content:encoded><![CDATA[<p>It is a common requirement to have pagination in Web applications. The easy way is to query all the records and then apply pagination logic to the data in memory. But this approach won&#8217;t work with tables which contains huge data sets.</p>
<p>The best approach is to do the pagination at database level using SQL. Database technology is an old and mature technology and hence this method is the most efficient one.</p>
<p>For example, let us assume that our application contains a CUSTOMER table and we want to display 15 customers at a time in a table. In Oracle, you can write a query like this one,</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> C<span style="color: #66cc66;">.*,</span>ROWNUM <span style="color: #993333; font-weight: bold;">AS</span> R <span style="color: #993333; font-weight: bold;">FROM</span> CUSTOMER C<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">WHERE</span> R <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #993333; font-weight: bold;">AND</span> R <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">10</span></pre></div></div>

<p>But what if you want to sort the customer data on the customer name and then want to display the first 10 records?</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> C<span style="color: #66cc66;">.*,</span>ROWNUM <span style="color: #993333; font-weight: bold;">AS</span> R <span style="color: #993333; font-weight: bold;">FROM</span> CUSTOMER C <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> CUSTNAME<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">WHERE</span> R <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #993333; font-weight: bold;">AND</span> R <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">10</span> </pre></div></div>

<p>But if you check the results, you will be in for a shock. The data will be wrong. This is because Oracle first applies the ROWNUM limit and then applies sorting on the limited set! So how do we get the first 10 customer after sorting on the customer name? That is where the analytical function RANK() comes to the rescue!</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> C<span style="color: #66cc66;">.*,</span> <span style="color: #66cc66;">&#40;</span>RANK<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> OVER <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> CUSTNAME<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> RN <span style="color: #993333; font-weight: bold;">FROM</span> CUSTOMER C<span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">WHERE</span> RN <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #993333; font-weight: bold;">AND</span> RN <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">10</span></pre></div></div>

<p>This approach doesn&#8217;t work in databases where there is no built RANK() function. For example, on AS400 systems only way to sort is to navigate to the required data window and then return only the records required. If you want records between 10 and 20, do a query and then navigate from 1 to 10, ignore records and then navigate from 10 to 20 and then break out of the query. But for large data sets navigating to the last page will take a lot of time.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingtips.org/sql/implementing-pagination-at-database-level-oracle-sql-use-rank.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>How do I execute scripts in the content retrieved using Ajax?</title>
		<link>http://www.codingtips.org/javascript/how-do-i-execute-scripts-in-the-content-retrieved-using-ajax.html</link>
		<comments>http://www.codingtips.org/javascript/how-do-i-execute-scripts-in-the-content-retrieved-using-ajax.html#comments</comments>
		<pubDate>Sat, 26 Apr 2008 19:11:32 +0000</pubDate>
		<dc:creator>codin</dc:creator>
		
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.codingtips.org/?p=13</guid>
		<description><![CDATA[Ajax enables Web application developers to build highly responsive Web interfaces. Ajax also comes with a lot headaches. One such problem arises when the content returned by an Ajax call contains JavaScript. By default this script won&#8217;t be executed by the browser and any JavaScript functions in the Ajax content won&#8217;t be available to the [...]]]></description>
			<content:encoded><![CDATA[<p>Ajax enables Web application developers to build highly responsive Web interfaces. Ajax also comes with a lot headaches. One such problem arises when the content returned by an Ajax call contains JavaScript. By default this script won&#8217;t be executed by the browser and any JavaScript functions in the Ajax content won&#8217;t be available to the main page.</p>
<p><strong>So how to execute JavaScript in the Ajax content?</strong></p>
<p>When you use Ajax, you first make an asynchronous call to server and the content returned is then assigned to a div tag using the innerHTML property as shown below.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;divId&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> ajaxContent<span style="color: #339933;">;</span></pre></div></div>

<p>In order to execute scripts inside the variable &#8220;ajaxContent&#8221;, we need to extract the script blocks and then pass to the JavaScript engine. So we will rewrite the above as follows,</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">setAjaxContent<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;divId&quot;</span><span style="color: #339933;">,</span> ajaxContent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Here is the pseudocode for the setAjaxContent method,</p>
<blockquote><p>1. Extract JavaScript method block by scanning for &lt;script&gt; tag.</p>
<p>2. Pass the JavaScript method block to browser JS engine by calling eval method.</p>
<p>3. Set the Ajax content to the div tag using innerHTML property.</p></blockquote>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> setAjaxContent<span style="color: #009900;">&#40;</span>divId<span style="color: #339933;">,</span> html<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
  <span style="color: #003366; font-weight: bold;">var</span> temp <span style="color: #339933;">=</span> html<span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> sindex <span style="color: #339933;">=</span> temp.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;script&quot;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;&gt;&quot;</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>sindex <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> eindex <span style="color: #339933;">=</span> temp.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;/&quot;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;script&gt;&quot;</span><span style="color: #339933;">,</span>sindex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> js <span style="color: #339933;">=</span> temp.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span>sindex<span style="color: #339933;">+</span><span style="color: #CC0000;">8</span><span style="color: #339933;">,</span>eindex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>           
    <span style="color: #000066; font-weight: bold;">eval</span><span style="color: #009900;">&#40;</span>js<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    temp <span style="color: #339933;">=</span> temp.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span>eindex<span style="color: #339933;">+</span><span style="color: #CC0000;">9</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>     
  document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>divId<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span><span style="color: #339933;">=</span>html<span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The above code scans for &lt;script&gt; blocks and then passes the script block to JavaScript engine using eval(). Finally the content is added to the innerHTML of the div you want to refresh.</p>
<p>This assumes that all the JavaScript&#8217;s in the content retrieved by Ajax is inside &lt;script&gt; and &lt;/script&gt; tags (without spaces or other attributes). For example, you will have to modify the function if you have the starting script tag as &lt;script type=&#8221;text/javascript&#8221; &gt;. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingtips.org/javascript/how-do-i-execute-scripts-in-the-content-retrieved-using-ajax.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>Creating a Website menu using unordered list and CSS</title>
		<link>http://www.codingtips.org/css/creating-a-website-menu-using-unordered-list-and-css.html</link>
		<comments>http://www.codingtips.org/css/creating-a-website-menu-using-unordered-list-and-css.html#comments</comments>
		<pubDate>Sat, 26 Apr 2008 03:13:55 +0000</pubDate>
		<dc:creator>codin</dc:creator>
		
		<category><![CDATA[CSS]]></category>

		<guid isPermaLink="false">http://www.codingtips.org/?p=8</guid>
		<description><![CDATA[Top level horizontal menus can be seen on a lot of Websites. Wondering how to quickly create one for your own site? If you are a theme developer(wordpress,joomla, drupal etc.), you will come across this need very frequently. There are many options to create horizontal menus, but the easiest and the most HTML friendly way [...]]]></description>
			<content:encoded><![CDATA[<p>Top level horizontal menus can be seen on a lot of Websites. Wondering how to quickly create one for your own site? If you are a theme developer(wordpress,joomla, drupal etc.), you will come across this need very frequently. There are many options to create horizontal menus, but the easiest and the most HTML friendly way is to use HTML unordered list (UL tag) and CSS. In this post I will show you how easy it is to create a menu using UL tag and CSS.</p>
<p><strong>1. As the first step create a menu structure using UL and LI tags</strong> - The following HTML code defines a sample menu structure. Note that the default UL behaviour is to list items vertically with bullets. </p>
<p> <img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="115" alt="ul menu stage1" src="http://www.codingtips.org/wp-content/uploads/2008/04/ul-menu-stage1.jpg" width="167" align="right" border="0" />
<div style="width: 420px">

<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;">html</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&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;#&quot;</span>&gt;</span>Home<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&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;#&quot;</span>&gt;</span>Subscribe<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&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;#&quot;</span>&gt;</span>About<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&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;#&quot;</span>&gt;</span>Contact<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</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>

</div>
<p><strong>2. Covert the menu to horizontal form</strong> - Before we can convert the ul/li tags to the horizontal form, we need to attach styles to it. In this case, I have enclosed the UL/LI in a div with class &quot;menu&quot;. You can then apply CSS to the styles as given below. As you can see it requires just 2 style attributes to change the menu to horizontal form. </p>
<p><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="60" alt="creating html menus using css2" src="http://www.codingtips.org/wp-content/uploads/2008/04/creating-html-menus-using-css2.jpg" width="255" align="right" border="0" /></p>
<div style="width: 420px">

<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;">html</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">style</span>&gt;</span>
.menu ul li { list-style:none;display:inline;}
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">style</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;menu&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&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;#&quot;</span>&gt;</span>Home<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&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;#&quot;</span>&gt;</span>Subscribe<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&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;#&quot;</span>&gt;</span>About<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&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;#&quot;</span>&gt;</span>Contact<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</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>

</div>
<p><strong>3. Add styles for links in the menu</strong> - Now we need to convert the links to look like a menu. The idea here is to increase the padding of anchor tag and then apply a different style when the mouse hovers over the link. We first apply float property on LI to remove unnecessary padding and to make it look like a menu. Then we apply disply:block to the anchor tag inside LI. The anchor tag will now occupy the complete space inside LI tag. We then add padding to the anchor tag to bring adequate space between menu items. Also anchor background color is changed when mouse hovers over the link. Here is it in action,<br />
<img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="56" alt="website menu using html and css3" src="http://www.codingtips.org/wp-content/uploads/2008/04/website-menu-using-html-and-css3.jpg" width="282" align="right" border="0" /> </p>
<div style="width: 420px">

<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;">html</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">style</span>&gt;</span>
.menu ul li { list-style:none;display:inline;float:left;}
.menu ul li a {padding:10px;background-color:blue;
      display:block;color:white;text-decoration:none;}
.menu ul li a:hover 
      {padding:10px;background-color:black;}
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">style</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;menu&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&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;#&quot;</span>&gt;</span>Home<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&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;#&quot;</span>&gt;</span>Subscribe<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&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;#&quot;</span>&gt;</span>About<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&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;#&quot;</span>&gt;</span>Contact<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</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>

</div>
<p><strong>4. Use images in the background for better visual effects</strong> - The menu is now complete. But you can give better visual effects if you use images instead of direct color codes for the background. Check out the following example which uses images for anchor backgrounds. In the hands of an expert Web designer the background images alone can create exceptional effects! </p>
<p><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="55" alt="website menu last preview" src="http://www.codingtips.org/wp-content/uploads/2008/04/website-menu-last-preview1.jpg" width="294" align="right" border="0" /> </p>
<div style="width: 420px">

<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;">html</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">style</span>&gt;</span>
.menu ul li { list-style:none;display:inline;float:left;}
.menu ul li a 
   {padding:10px;background:url(bg.png);display:block;
   color:#990000;text-decoration:none;font-weight:bold;}
.menu ul li a:hover 
   {padding:10px;background:url(none) black;}
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">style</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;menu&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&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;#&quot;</span>&gt;</span>Home<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&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;#&quot;</span>&gt;</span>Subscribe<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&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;#&quot;</span>&gt;</span>About<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&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;#&quot;</span>&gt;</span>Contact<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</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>

</div>
]]></content:encoded>
			<wfw:commentRss>http://www.codingtips.org/css/creating-a-website-menu-using-unordered-list-and-css.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>Confirm closing of browser window using onbeforeunload</title>
		<link>http://www.codingtips.org/javascript/confirm-closing-of-browser-window-using-onbeforeunload.html</link>
		<comments>http://www.codingtips.org/javascript/confirm-closing-of-browser-window-using-onbeforeunload.html#comments</comments>
		<pubDate>Fri, 25 Apr 2008 17:45:54 +0000</pubDate>
		<dc:creator>codin</dc:creator>
		
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.codingtips.org/?p=3</guid>
		<description><![CDATA[While using a Web application, users may accidently close the browser by clicking on &#34;close button&#34;. For application windows which are used for extensive data capture, it is better to provide a confirmation message before the browser is closed. This is where onbeforeunload event of body/window object can be used.
An event handler JavaScript function can [...]]]></description>
			<content:encoded><![CDATA[<p>While using a Web application, users may accidently close the browser by clicking on &quot;close button&quot;. For application windows which are used for extensive data capture, it is better to provide a confirmation message before the browser is closed. This is where onbeforeunload event of body/window object can be used.</p>
<p>An event handler JavaScript function can be assigned to onbeforeunload event. If a value is returned from this function, browser will ask for a standard confirmation message. The returned value will also be displayed in this confirmation message. Consider the following HTML page,</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>html<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>script<span style="color: #339933;">&gt;</span>
	<span style="color: #003366; font-weight: bold;">function</span> preventClose<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;Message from onbeforeunload handler&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	window.<span style="color: #660066;">onbeforeunload</span> <span style="color: #339933;">=</span> preventClose<span style="color: #339933;">;</span>
	<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></div></div>

<p><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="150" alt="onbeforeunload confirmation message" src="http://www.codingtips.org/wp-content/uploads/2008/04/onbeforeunload-confirmation-message2.jpg" width="369" align="right" border="0" /></p>
<p>When you try to close browser window with the above html file loaded, you will get the confirmation message shown on the right.</p>
<p>But there is a problem here. If your page has anchor tags with JavaScript method attached to href, onbeforeunload is called even before executing the href script! This is because browser considers href navigation as moving to a different page. For example clicking on the link given below triggers onbeforeunload,</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;javascript:onSave()&quot;</span>&gt;</span>Save<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 solution in this case is to attach JavaScript to an anchor tag using onclick() event. Here is an example,</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;">onclick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;onSave()&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span>&gt;</span>Save<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>This won&#8217;t trigger a onbeforeunload unless you submit form or navigate to another page from the onSave() method.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingtips.org/javascript/confirm-closing-of-browser-window-using-onbeforeunload.html/feed</wfw:commentRss>
		</item>
	</channel>
</rss><!-- Dynamic Page Served (once) in 0.694 seconds -->
