Skip to content

Categories:

Detect non US-ASCII character entry in a text input field

How would you detect arabic character entry in a text input field with the help of javascript? Recently I had to do this in one of my project and I was doing some research for this. Thanks to Pablo for a great piece of information on this.

“First 128 characters (US-ASCII) need one byte. The next 1,920 characters need two bytes to encode. This includes Latin letters with diacritics and characters from Greek, Cyrillic, Coptic, Armenian, Hebrew, Arabic, Syriac and Tāna alphabets. Three bytes are needed for the rest of the Basic Multilingual Plane (which contains virtually all characters in common use). Four bytes are needed for characters in the other planes of Unicode, which include less common CJK characters and various historic scripts.” – Wikipedea.

So arabic letters are double bytes and english letters are single byte characters. Just need to write a javascript function to find out the bytecount of the text

 function  isDoubleByteCharacter(Text){
        var countMe = Text;
        var escapedStr = encodeURI(countMe);
        if (escapedStr.indexOf("%") != -1) {
            var count = escapedStr.split("%").length - 1;
            if (count == 0) count++  //perverse case; can't happen with real UTF-8
            var tmp = escapedStr.length - (count * 3);
            count = count + tmp;
        } else {
            count = escapedStr.length;
        }
       // if the text is US-ASCII then the total byte count will be equellent to the length of the text
        if (count > Text.length){
		return true; // yes its non US-ASCII text
	}
	else{
		return false; // no it's not US-ASCII text
	}

Posted in Web development.

Tagged with .


Opening KMZ file in your browser using Google Map API

Google Map can read KML/KMZ files, so you can share your data created in google earth with google map. This is handled by GGeoXml object.

Here’s an example how to view data from a kml/kmz file using google API

    var map;
    var geoXml;
    function initialize() {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(0,0));
        geoXml = new GGeoXml(hostname + kmzfile, function() {
         zoomToGeoXML(geoXml); // centering the map according to the kml data
        });
      }
    } 

    //Zoom the map
    function zoomToGeoXML(geoXml) {
        var center = geoXml.getDefaultCenter();
        var span = geoXml.getDefaultSpan();
        var sw = new GLatLng(center.lat() - span.lat() / 2,
                        center.lng() - span.lng() / 2);
        var ne = new GLatLng(center.lat() + span.lat() / 2,
                        center.lng() + span.lng() / 2);
        var bounds = new GLatLngBounds(sw, ne);
        map.setCenter(center);
        map.setZoom(map.getBoundsZoomLevel(bounds));
        map.addOverlay(geoXml);
    }

and here is a working example

This page outlines KML support in Google Maps and Google Maps for mobile.

Posted in Web development.

Tagged with .


Jquery – Ajax : Reading from xml and generating html

I was recently working on a project which requires parsing html from a .net service return Xml. It’s pretty simple technique and a useful thing for any javascript/ajax developer

First I will demonstrate how to parse html from xml

Here’s the xml structure which a .net service returns

<properties>
 <property>
		<id>18</id>
		<city>Abu Hamourq</city>
		<type>Villa</type>
		<long>25.1321</long>
		<lat>58.13131</lat>
		<price>10000 QAR</price>
		<image>/Images/Property/16/thumbnail/thumbnail2.jpg</image>
	</property>
 <property>
		<id>19</id>
		<city>Abu Hamour2</city>
		<type>Apartment</type>
		<long>25.1321</long>
		<lat>58.13131</lat>
		<price>10000 QAR</price>
		<image>/Images/Property/16/thumbnail/thumb1.jpg</image>
	</property>
	</properties>

and below the html structure needs to be generated

<ul>
	 <li>
		 <div class="image"><img src="/Images/Property/16/thumbnail/thumbnail2.jpg" /></div>
			<div class="Desc">
			 City : Abu Hamourq<br />
				Type : Villa<br />
				Price : 10000 QAR<br />
				Latitude : 25.1321<br />
				Longitude : 25.1321<br />
			</div>
	 </li>
	 <li>
		 <div class="image"><img src="/Images/Property/16/thumbnail/thumb1.jpg" /></div>
			<div class="Desc">
			 City : Abu Hamour2<br />
				Type : Apartment<br />
				Price : 10000 QAR<br />
				Latitude : 25.1321<br />
				Longitude : 25.1321<br />
			</div>
	 </li>
	</ul>

the javascript function which calls the service and loops through the xml and parse html in a

    list

    function getDatafromService(){
    
    		/*.net  service url which returns the xml, the querystring values filters the result*/
    	 var xmlServiceURI = "/getData.svc?filterstring=filterstringval"; 
    
    		/*variable to hold html markup */
    		var htmlMarkup = "<ul>"; 
    
    		/* jquery ajax function */
    		$.ajax({
       type: "GET",
       url: xmlServiceURI,
       dataType: 'xml',
       success: function(xmlData){
    				$(xmlData).find("property").each(function(){
    	    htmlMarkup = htmlMarkup + "<li>" +
    		   htmlMarkup = htmlMarkup + "<div class=/"image/"><img src=/""+ $(this).find("image").text() +"" /></div>"+
    			  htmlMarkup = htmlMarkup + "<div class=/"Desc/">"+
    			  htmlMarkup = htmlMarkup + "City : "+ $(this).find("city").text() +"<br />" +
    				 htmlMarkup = htmlMarkup + "Type : "+ $(this).find("type").text() +"<br />" +
    				 htmlMarkup = htmlMarkup + "Price : "+ $(this).find("price").text() +"<br />"+
    				 htmlMarkup = htmlMarkup + "Latitude : " + $(this).find("lat").text() + "<br />"+
    				 htmlMarkup = htmlMarkup + "Longitude : " + $(this).find("long").text() +"<br />"+
    			  htmlMarkup = htmlMarkup + "</div>" +
    	    htmlMarkup = htmlMarkup + "</li>";
    				});
    
    				/* closing ul */
    				htmlMarkup = htmlMarkup  +"</ul>";
    
    				/* appending the markup to  <div id="Content"> in the page  */
    				$("#Content").html(htmlMarkup);
    	 },
    		error:function(){
    		 $("#Content").html("Sorry there's an error ');
    		}
    	}
    

    Hope it helps.. Cheers!

Posted in Web development.

Tagged with , , , .


Binding a Hyrarchial Dataset to an ASP.net Treeview control

Recently I was doing some practice in asp.net data access layer. It is great technique to access your database objects.

I was trying to bind my hyrarchial database table (recursive table) to an asp.net treeview control and I wanted something very simple. Interestingly I couldn’t find any direct databinding methods for a treeview to the recursive table. Atleast there’s no simple method to make the binding with the treeview (correct me if I am wrong) unlike other controls like datagrid, datalist etc.Since ASP.net Treeview understands only hyrarchial dataset, I have to find a way to convert my dataset to a hyrarchial dataset.

On further dig, I found a nice article by Ralph Varjabedian. He wrote an ASP.net class to convert the dataset to a hyrarchial dataset. Thanks to Ralph!

And finally I was successfull to bind the control with the dataset in the way I wanted.

Here is the Code I wrote

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DataSet1TableAdapters;

public partial class Campaigns : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{   DataSet dataSet = new DataSet(); //Dataset
TableAdapter CampaignTableAdapter = new TB_campaignTableAdapter(); // data table adapter
dataSet.Tables.Add(CampaignTableAdapter.GetData()); // populating the dataset
tvCampaign.DataSource = new HierarchicalDataSet(dataSet, "ID", "id_parent", 0); // converting to hyrarchial dataset
tvCampaign.DataBind();
tvCampaign.CollapseAll();            // Collapsing all nodes by default
}
}

And I am interested to learn if there’s any other better solution for this.

Posted in Web development.


Lavalamb effect with jquery easing plugin

Here’s a small Javascript function which enables animated highlighting effect on the navigation (lavalamb effect) with the help of jquery easing plugin

$.fn.lavalamb = function(ImgPath){
/* ImgPath:  Path of the image which you need to animate when you hover over a navigation link
   this can be a shadow png image or a pointer image or underline image which you want to animate with a mouseover / mouseout action on a link

   Add css class "Highlight" to the node which is currently active, so the image will always be animated back to this node on mouseout
*/
var LeftPos;
$(this).append('<div id="lavalamp"><img src="'+ImgPath+'" alt="" /></div>'); //Appending lavalamb image to the html, this div can be skinned in the css.
$(this).parent().css({position: "relative"}); //setting the position
$("#lavalamp").css({
position: "absolute",top: $(this).height() // These css values can be edited to vertically position the lavalamb image
});
  $(this).find("li").hover(  //Link hover function
function(){ 	//Rollover
LeftPos = $(this).offset().left - $(this).parent().offset().left; //calculating the left position
LeftPos = (LeftPos - ($("#lavalamp").width()/2) + ($(this).width()/2)); //calculating the left position
$("#lavalamp").animate({left: LeftPos},{queue:false,duration:700,easing: "easeInQuart"}); //Animating the image from the start position to the current mouseover position
},
function(){
   LeftPos = $(this).parent().find("li.Highlight").offset().left - $(this).parent().offset().left; // Getting the position of the highlighted node
 	 $("#lavalamp").animate({left: LeftPos},{queue:false,duration:700,easing: "easeOutQuart"}); //Animating the image back to the start position from the current mouseover position
  });
}

And call this function in the html page

<script type="text/javascript">
$("#Navigation").lavalamb("Images/Background/NavShadow.png"); // Navigation is an id of the <UL> list which has navigation nodes
</script>

lavalamb

Here’s an example
Example1

Posted in Web development.


Javascript sound manager API

Browsers lack good, consistent native audio/video support. (HTML 5’s audio and video are on the way, but not universal yet.)
Here’s Sound Manager 2, a nice Javascript API via Flash + ExternalInterface to add audio & video to your website.

http://www.schillmania.com/projects/soundmanager2/doc/getstarted/#intro

Demo Audios
http://www.schillmania.com/projects/soundmanager2/demo/page-player/

Demo Videos
http://www.schillmania.com/projects/soundmanager2/demo/video/

here is a live example …
http://paris.conciergerie.com/

Posted in Web development.


Skinning HTML form elements

It’s always a pain to skin the normal radio/checkboxes and the select lists by using CSS.
I was doing some research on this and figured out a way using javascript/css. I put the functions together in a small jquery plugin.

My idea was to customize the radio/checkbox/listboxes without disturbing the actual markup.
The solution makes use of real radio buttons with labels and their toggle functionality and skinned list-items, allowing radiobutton list rendering and easy skinning.

skindefault

In the above form thre are a few dropdown lists, radio button and checkboxes. The markup looks like

 <fieldset class="Dropdown">
	<select class="NonJsSelect">
	 <option>List Item 1</option>
	 <option>List Item 2</option>
	 <option>List Item 3</option>
	 <option>List Item 4</option>
	 <option>List Item 5</option>
	 <option>List Item 6</option>
	 <option>List Item 7</option>
	 <option>List Item 8</option>
	 <option>List Item 9</option>
	 <option>List Item 10</option>
	 <option>List Item 11</option>
	</select>
 </fieldset>
 <p><strong>Checkbox controls</strong></p>
 <fieldset class="CheckList">
 	<input type="checkbox" id="CheckBox1" class="Checkbox" />
 	<label for="CheckBox1">Checklist 1 value </label>
 </fieldset>
 <fieldset class="CheckList">
 	<input type="checkbox" id="CheckBox2" class="Checkbox" />
 	<label for="CheckBox2">Checklist 2 value </label>
 </fieldset>
 <fieldset class="CheckList">
 	<input type="checkbox" id="CheckBox3" class="Checkbox" />
 	<label for="CheckBox3">Checklist 3 value </label>
 </fieldset>

Just wrap your controls in a <fieldset> or a <div> and add css classes “Dropdown” for comboboxes and “CheckList” for radio/checkbox controls as shown above.

Download the css/javascript files then add the javascript javascript css references to the page.

Finally just initiate the javascript function

<script type="text/javascript">
$.function(){
 $("select").AddSkin();
 $(".Checkbox").AddSkin();
//you can reference the controls by its name, id or css class just like any other jquery calls.
}
</script>

skin_1
You can change the skin by editing the CustomControls.css and replacing the images.

here’s an example

Try it out!

You can download the sample code and plugin from here.

Note: The plugin currently supports only Dropdown, checkbox and radio button controls.
I order to make this work you have to include the latest jquery library.

Posted in Web development.

Tagged with .


Mysore Palace

In my recent trip to India, I got a chance for a short visit to Mysore Palce, a priceless national treasure and the pride of a kingdom, the Mysore Palace is the seat of the famed Wadiyar Maharajas of Mysore.

An eclectic synthesis of architectural styles the palace is one of India’s most dramatic national monuments. Today it is a museum housing treasures from across the world reflecting the rich and colorful history of the erstwhile princely state of Mysore.

palace1

The building you see today was constructed in 1912 by the royal family of  Mysore the Wadiyars. It was a well built,  neatly maintanied and tightly secured (you can’t even take you cameras inside ) palace in India. And it’s really impressive to see  that the government has been doing a brilliant job in retaining it’s pride and beauty as it had before.

Definitly a must see place!

Posted in Travel.

Tagged with , .


Avoiding web page zoom in the iPhone

Just found a cool tip to aviod default page zoom in iPhone . Just add the following tag

<meta name=”viewport” content=”width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;”/>

on further dig I found some useful apple development guides describes about the iphone viewport and customizing style-sheets

http://developer.apple.com/iphone/library/documentation/AppleApplications/Reference/SafariWebContent/UsingtheViewport/UsingtheViewport.html


http://developer.apple.com/iphone/library/documentation/AppleApplications/Reference/SafariWebContent/AdjustingtheTextSize/AdjustingtheTextSize.html

Posted in Web development.