Tag: html

  • Fixed footer auto height content

    While redesigning one of my pet projects – Simplememos – I came across a very pesky issue on layouts. I wanted a simple header/content/footer layout, with some specific behaviors. Header is part of content, content is using up 100% of whatever area is left after positioning footer fixed at bottom. Sounds easy? Well, what if the content has nothing inside? Yes, all of my content inside was absolutely positioned ‘notes’ that meant content had nothing by which it can auto-size itself! I also needed to have proper visual layout for all screen sizes, handle browser resize, maximize, restore for ALL browsers.

    The simple answer, yes I have that layout. Take a look at Simplememos, and feel free to play around in any browser of your choice.

    From what I experienced, played around, researched, and figured out eventually was that there is no CSS way of doing this. My own philosophy is to write less, and let CSS handle the details of my layouts as I have specified. But as many seasoned web players know, browsers come with all variations of CSS interpretations, and then you see all those threads on stack overflow around layouts, you’ll definitely have some fix, but then again it won’t work for your specific case if you had like the one I had.

    I can’t say stack overflow wasn’t of any help. In fact, I gathered many useful points from across a variety of stack overflow posts, replies, code snippets, and eventually I came up with a version that works for my own needs.

    My implementation was quite simple, but is a mix of CSS and JS. I couldn’t avoid JS, unless I go with CSS expressions which in a nutshell is JS to me.

    I created 2 regions as below-
    <div id="content">... my header, content inside ...</div>
    <div id="non-content">... my footer here ...</div>

    My div#non-content auto expands to my footer, since footer is a text. However, my div#content has only absolutely positioned divs, so it has no auto-expand criteria as such. Thus, I jig up jQuery’s document.ready event to dynamically resize div#content

    $(document).ready(
    div#content's height= (browser window's height - div#non-content's height)
    )

    And I also hook the window’s resize event to do the same dynamic height change again.
    $(window).resize(
    // Same code to dynamically resize div#content to occupy 100% of remaining available width
    div#content's height= (browser window's height - div#non-content's height)
    )

    There are some additional things I could have done
    1. Include a spacer image img#spacer-height that is hidden but dynamically resized to occupy maximum available height, and another image img#spacer-width to occupy maximum available width. I have used this for IE6 and below one time to handle overflow:auto for div#content. Without any content, setting width, and height is OK, but IE6 never bothers to show up any scrollbars.
    $('.spacer-width').width= window's width
    $('.spacer-height').height= (window's height - div#non-content's height)

    2. Use tables! This would have been the best option, but sadly FF does not support overflow:auto’s in table cells. As alternatives I didn’t bother to emded div’s, because I had already got to the point where I am having tables, divs, CSS, and JS to resize the div anyways.

    You can see my source in action on my own Simplememos site. If you have pure CSS alternatives, I’ll be more than happy to hear!

    Aside from the above, I must say the experience of using Knockout JS, custom KO bindings, jQuery, and extending jQuery selectors was a rewarding experience. I have these, and quite a number of stack overflow posts open for quite a few months now.

  • Setting tabindex in HTML for arbitrary, disparate elements

    I hit upon a picky need by a user to have specific tab-order for a HTML page. For most part we usually would organize the layout itself to allow for default left to right, top to bottom tabbing of elements. Or we could always explicitly set tabindex to specific order.

    Trouble was, having limited capability to influence the HTML generated by the package, the only option was do some post UI alteration using JS, or CSS only. Not surprisingly, CSS is about formatting, not navigation hence there isn’t any CSS property for tabindexes. The only option in hand was JS, so I was playing with a couple of approaches to look at the problem

    • Request user to live with the default tab order – Not an option 🙂
    • Use CSS – Not possible to set navigational properties such as tabindex
    • Use JS
    • Hack onfocus, onblur to attach/de-attach custom tabindex – didn’t work for set of controls, since one can enter any control using mouse, still custom tab order needs to be maintained. Another issue was controls appearing after custom tabindexed controls didn’t get any focus any more.
    • Use onkeyup, onkeydown events, detect TAB – didn’t work because onkeydown, onblur occurred on element having focus, then onfocus, keyup occurred on element receiving focus. While logically correct, my sample code quickly became a mess of booleans tracking keyup and tab key, then clearing it onfocus, but again similar to previous issue this didn’t work either.
    • From a completely different perspective, surround required elements in containers, like div, span or fieldset. This would automatically take care of tab order, but trouble is the elements I had to work on were in table row cells, and HTML doesn’t allow interleaving of containers (which is logical).
    • Had to work simply across all browsers without differences in implementation. I came across some posts suggesting setting required tab order through java script for all known elements. Another post improved this thought by suggesting to have tabindexes with incremental gaps say 10, so that additional elements could be put in tabindex later on.
    • So my final working approach took both of the above, and mistakes in approach to come up with a very simple solution as below
      <script type="text/javascript" language="javascript" src="prototype-1.7.min.js"></script>
      <script type="text/javascript" language="javascript">
      //<![CDATA[<!--
      // Requires prototype.js
      document.observe("dom:loaded", function() {
      /* Specify elements in order of required tab index, cte= custom tab elements */
      var cte= $('select7', 'select9', 'select10', 'searchkb', 'select8');
      /*
      * Logic to set custom tabindexes for elements
      * First set tabindexes for all known input-able elements in with gaps in between
      * For required elements, set tabindex in that gap range
      */
      var idx= parseInt(1000), incr= parseInt(10); // Begin with any suitable base number, idx= index, incr= suitable increment gap to allow assigning tabindexes in sequence
      $$('input', 'select', 'option', 'textarea').each(function(el) { try { el.writeAttribute('tabindex', idx+=10); } catch(err) { void(0); } });
      var fi= parseInt(cte[0].readAttribute('tabindex')); // fi= tabindex of first element
      cte.each(function (el) { el.writeAttribute('tabindex', fi+=1); }); // Rest of the elements receive sequential tabindex
      });
      //-->]]>
      </script>

    I was leaning on using jQuery’s attr to write tabindex, but for some reason (I know that’s not an excuse) it didn’t work. Perhaps it should, however prototype’s writeAttribute, readAttribute worked, so I ended up using that. Also note that not all elements support tabindex,  though I’ve limited to input-able elements, so setting this throws error which I’ve deftly gobbled up; not a good practice but if someone knows an elegant way to find if element supports some property that would be great. On other hand, I would prefer jQuery, or prototype hiding such intricacies behind their APIs instead of me having to do it since it’s javascript anyways. The try, catch could be altogether removed though.

    BTW, I used YUI’s excellent javascript compressor to minify prototype’s javascript. Google’s CDN didn’t provide a minified prototype.js, I wonder why …?

    I believe my goal of having a simple, yet elegant way to override tabindex, taborder especially if you have no control over HTML was achieved. Perhaps this helps you in some way as well.

  • Spring IoC, DI quick tutorial

    There has been an (evident) craze amongst Java community with contrived terms such as IoC (Inversion of Control), DI (Dependency Injection) mainly through Spring. I initially found the connotation “Don’t call me, I’ll call you” a bit difficult (yeah, it’s bending your head upside down) to understand, but after spending few hours around Spring documentation, I get the gist.

    To a layman (Java layman, of course) it is helpful to picture that each Java program is a set of one or more classes, which act together. Essentially, this means classes are “dependent” on some classes in order to be fully functional. Usually, the class requiring functionality of another class instantiates the class, and uses it. This is called coupling because class instantiates the object of required class. What if we always got an “instantiated” instance of required class, and our class did not have to worry of instantiation? – This is called IoC (Inversion of Control) principle in Spring, and it achieves this by providing ready-to-use instance (injecting dependency) to your class. This has some important uses, since now you don’t worry of creating connections to databases, loggers to log4j, or sessions for JMS queues. Spring will create these for you, and your class can focus on the actual purpose – using the pre-instantiated ready-to-use object.

    Get it? OK, to simplify it further, let us assume you have a class A having one field – log. You want to use log to log information, but your class nowhere has the logic to instantiate or initialize log. You accept a pre-instantiated log through constructor, or getter/setter methods, and you will have a ready-to-use log instance passed to your class via Spring!

    I won’t go into details of Spring further, since Spring’s own documentation on http://static.springsource.org/spring/docs/2.5.x/reference/index.html is the definitive source to look into.

  • Javascript sprite animations

    Javascript sprites weren’t as difficult as I thought. With a little bit of help, math, some css, js, images (of course) a decent animation can be shown without using any flash at all. I coupled it in a small class Animator, and wrote a small demo for anybody interested in using it.

    http://www.naiksblog.info/jsspriteanim/index.html

  • My first website – nostalgic

    I stumbled yesterday upon my first website I had created. This sure brings back memories on my old desktop PC, and keyboard sitting, and typing away at late nights 😉
    http://www.naiksblog.info/shantibhushan/default.html

    On another note, it does have some useful notes on Chasen (Japanese language morphological analysis tool), Mecab, Using Python with Chasen, Cygwin etc.

  • Outlook export to Thunderbird

    There is no straight (should I say “easy”) way to export Outlook mails to Thunderbird. Using Thunderbird’s import from Outlook did not work since quite a few emails came up as raw HTML, and I had to manually change Automatic encoding detection to OFF, Universal to see the contents each time.
    A sure-shot (well… which took me 99% of the) way was Outlook –> Import into Outlook Express –> Import into Thunderbird. This worked, but with one sad issue – non-English attachment names are not as original. If you can live with that, then this is the surest way to go !

    Few references which helped-
    Import .pst files – MozillaZine KB

  • Whois purely in SSI

    It was fun learning SSI, and tweaking around to get useful information such as detecting your IP adddress, and further WHOIS information.

    See below in action

    http://www.naiksblog.info/ip2country.shtml

    Impressive how quickly a page could be constructed using SSI. Courtesy to Apache’s mod_include, IP to Country database, and APNIC references.

  • PHP in UTF-8

    To setup PHP for UTF-8

    データベース、スクリプトを記述するファイルの文字エンコーディングはPHPの文字エンコーディングを使用する方が設定が行い易いです。

    default_charset=”UTF-8″

    ダイナミックコンテンツの文字コードセットは必ず指定しなければならない。(セキュリティ上の理由。詳細は 2000年2月のCERTのXSSアドバイザリを参照)

    magic_quotes_gpc=off

    マルチバイト文字エンコーディング環境のみでなく、セキュリティ上も有害であるので必ずoffに設定する。ポータブルなスクリプトの場合、この設定 がonである場合にstrip_slashes()を全ての入力に適用するコードをスクリプトの開始時に実行する。稀に magic_quote_gpc=onである事を前提としているアプリケーションもある。そのようなアプリケーションは使わない方が良い。

    mbstring.input_encoding=”pass”

    現在のブラウザでHTMLを記述した文字エンコーディング以外で文字を送信してくるようなブラウザはない。(携帯などモバイル環境を除く)

    mbstring.internal_encoding=”UTF-8″

    ブラウザから送信される文字エンコーディングはcharsetと同じはず。プログラム側では必ず送信された文字 エンコーディングが正当なUTF-8エンコーディングであるか確認する事。

    mbstring.output_encoding=”pass”

    出力はinternal_encodingで行われる。つまりUTF-8。(携帯などモバイル環境を除く)

    mbstring.language=”japanese”

    言語環境を日本語に設定。mb_send_mail関数などの動作に影響する。

    mbstring.substitute_charactor=””

    入力に変換出来ない文字エンコーディングを含む場合、アプリケーションの実行を停止しなければならない。本来、セキュリティ上不正な文字を削除すべ きではないが、古いPHP(PHP 4.4.2, 5.1.2以下)では文字エンコーディングを確認する仕組みが無い。古いPHPで効率的に不正エンコーディングを検出するには文字列の長さの変化で確認す る。*1

    PHP 5.1.3, 4.3.3以降はmb_check_encoding関数を利用してスクリプトを実行の初期段階で不正エンコーディングを検出することが望ましい。

  • Racking brains – a Javascript string combination generator

    After racking my brains for almost 4 days (yeah I am a slow learner) I finally created a simple Javascript string combination generator

    See the demo http://naiksblog.info/stringcombinations.html

    I tried modeling the logic to how databases combine sets in a cross join. All rows from left side are combined with all rows from right side.

    The script is as below

    	function combine(a, b) {
    		var r= new Array();
    		for(var i= 0, k= 0; i < a.length; i++) {
    			for(var j= 0; j < b.length; j++) {
    				if(-1==a[i].indexOf(b[j])) r[k++]= a[i]+ b[j];
    			}
    		}
    
    		return(r);
    	}
    

    You can call this as below

    	function permute() {
    		var a= "abcd";
    		var p= new Array();
    
    		for(var i= 0; i< a.length; i++) p[i]= a.charAt(i);
    		var r= p;	// Input string as-is is first permutation
    		for(var i= 1; i< a.length; i++) r= combine(r, p);	// Get the permutations
    		// r.length - Get the combinations
    		// r contains all combinations as an array
    	}
    

    Some points worth noting

    • If any character is repeated, the combination does not happen successfully since the logic tries to remove same character matching elsewhere
    • The number of combinations increase by factorial of the number of characters, hence it will be a good idea to perform this on server side ideally otherwise javascript will hang for large strings
    • Logic could be optimized to generate combination in a different better way than using crossjoin strategy