Archive for July, 2011

Yes, there are loads of context menu plugins already. But they require a fair amount of work to make them look good. This one is easy to use, small, and looks good.

joewalnes.com/2011/07/22/a-simple-good-looking-context-menu-for-jquery/

Advertisements

Scriptographer ported to JavaScript and the browser, using HTML5 Canvas.

paperjs.org

You all must have heard about HTML5 the new standards of markups.

HTML5 is a cooperation between the World Wide Web Consortium (W3C) and the Web Hypertext Application Technology Working Group (WHATWG).

WHATWG was working with web forms and applications, and W3C was working with XHTML 2.0. In 2006, they decided to cooperate and create a new version of HTML.

Some rules for HTML5 were established:

  • New features should be based on HTML, CSS, DOM, and JavaScript
  • Reduce the need for external plugins (like Flash)
  • Better error handling
  • More markup to replace scripting
  • HTML5 should be device independent
  • The development process should be visible to the public

It reduces the amount doctypes, encoding and meta tags on your webpage, have introduced lot of new markup tags such as <article>, <canvas>, <footer>, <Video>, geolocation etc. which makes your webpage source much more understandable for the search engines.

Also, It looks much more cleaner than the HTML4 / XHTML

HTML5 development is still under progress, and expected to be a public standard by 2020 or 2022.

Most of the latest browsers have started adding HTML5 support, however all the HTML5 tags are not available till date, Google Chrome supports most of the HTML5 tags, Firefox supports few and IE supports the least.

New tags are good, but the most userful feature in HTML5 I found is the local / web storage. It is a good replacement of browser cookies to store anything at client side.

One of the common disadvantage of cookie is that they are included with every HTTP request, thereby slowing down your web application by needlessly transmitting the same data over and over.

How to use localstorage in HTML 5:

Localstorage:

<script type=”text/javascript”>

localStorage.lastname=”Smith”;

document.write(localStorage.lastname);

</script>

Sessionstorage:

<script type=”text/javascript”>

sessionStorage.lastname=”Smith”;

document.write(sessionStorage.lastname);

</script>

Try this out, so far i have tested it on IE8, mozilla FF 4.0 & 5.0, Safari, Chrome, Opera and Rockmelt, they do support this local storage feature.

For more details on HTML5 and localstorage visit:

w3schools.com/html5/html5_webstorage.asp

diveintohtml5.org/storage.html

diveintohtml5.org

Another useful feature of delegates is the ability to execute a method asynchronously. That is, through a delegate, you can begin invocation of a method and then return immediately while the delegate executes its method in a separate thread. Your page execution does not need to wait for that method to complete.

The following example demonstrates the use of delegates for asynchronous method invocation. To see this in action we need a method that simulates a lengthy operation, such as writing data to db logs, record page hits that can be asynchronous, and do not affect the end client response.

public delegate void LongTimeTask_Delegate(string s);

protected void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{

// databind of all the controls

LongTimeTask_Delegate d = null;
d = new LongTimeTask_Delegate(LongTimeTask);

IAsyncResult R = null;
R = d.BeginInvoke(“TestString”, null, null); //invoking the method
}
}

In the above load event of the page after binding all the UI controls we need to perform a lengthy operation to record something in DB or any IO operation file.
We are initializing the deligate with the LongTimeTask method, and delegate.BeginInvoke will call the method below asynchronously. Later we will see how to know if the method invokation completed successfully or not.

public void LongTimeTask(string s)
{
// Write your time consuming task here
}

Now, if we would want to perform an another operation after the above method is completed, That is called callback approach, add the async call back event in beginInvoke:

//using callback approach
R = d.BeginInvoke(“TestString”, new AsyncCallback(TaskCompleted), null);

public void TaskCompleted(IAsyncResult R)
{
// Write here code to handle the completion of
// your asynchronous method
}
The above method will execute on completion of LongTimeTask method.

We can also wait untill the LongTimeTask is execution is completed or not, but then that will not serve the purpose of calling it asynchronously, but still you would need it somewhere:

//this will block until the asynchronous operation is complete
d.EndInvoke(R);

Try it out at your end, I have just given you the skeleton that I have tried, It works fine, but just be careful, as this works on a separate thread, which might sometimes consumes a lot of memory / CPU usage.

I was developing an application to post xml data to a URL (REST call) using asp.net and C#. The endpoint was hosted with a third party vendor and they had a much secured hosting environment. Their endpoints were https and they installed and generated the private client certificate (a password protected .p12 file) on their servers to prevent unwanted requests / hits.

So to access their endpoints one has to have that client certificate, basically any request to the endpoints should have the client certificate enclosed with the request data, even if you are requesting it from browser window you have to have that certificate installed in your browser.

To resolve the same first I installed that certificate on my development server, then I could able to request that URL from browser window. But when I did the same using my asp.net C# code using httpwebrequest object I got 403 forbidden error as the request was not sending the client certificate with xml data.

Ideally, if you have any .cer file you can add it with request object by using X509Certificate class but in case of private client certificate it just doesn’t work for that you have to use X509Certificate2 class, Here is the code snippet. You need to place the .p12 file in your webserver’s hard drive.

public string PostData(string DataToPost,string URL)
{
String result = “”;
String strPost = “RequestXML=” + DataToPost;
StreamWriter myWriter = null;

HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(URL);
objRequest.Method = “POST“;
objRequest.ContentLength = strPost.Length;
objRequest.ContentType = “application/x-www-form-urlencoded”;

objRequest.ClientCertificates.Add(new X509Certificate2(@“E:\Certificate\clientcertificate.p12”, “password”));

try
{
myWriter = new StreamWriter(objRequest.GetRequestStream());
myWriter.Write(strPost);
}
catch (Exception e)
{
//
}
finally
{
myWriter.Close();
}

HttpWebResponse objResponse = HttpWebResponse)objRequest.GetResponse();
using (StreamReader sr =
new StreamReader(objResponse.GetResponseStream()))
{
result = sr.ReadToEnd();

// Close and clean up the StreamReader
sr.Close();
}

return result;
}

Wanted to share an unfortunate experience, I lost my brand new Samsung Galaxy ace android phone near my place, same day I went to the nearest police station to lodge an FIR. Respected Police officer didn’t lodge the complain but asked for a letter from my service provider, mentioning that the SIM card belonged to me. I told him I dont want to file an FIR for SIM card, I want to file it for my phone, Sim card i can get it blocked, and anybody who has stolen my phone was not using my sim, he/she has already switched off the phone. But police man didn’t listen so I had to leave to get the required letter.

I went to Vodafone gallary and explained the situation, the store owner said he can block the sim right away but will not provide any such letter that the SIM card belonged to me. I got the sim blocked, and requested him for the letter but he again denied and said Vodafone doesn’t provide any such letter. I called up vodafone customer care as well, but got the same response.

I returned back to the police station told them that Vodafone doesn’t provide any such letter and requested the officer to lodge the FIR for the handset, I had the bill of handset too, officer said the complaint wont be recorded unless I bring the letter from Vodafone. I had to leave. He also showed me few past applications from other victims, they had submitted the letter from their service providers.

I knew that my phone is gone but I still needed to file this FIR just in case in future if anyone misuses my phone but these bunch of fools were driving me crazy, it was my phone, my sim, i had every possible and required document with me, but still they were not helping me out.

I had no choice but to write a harsh email to vodafone nodal officer and mumbai appeal center. In that email I explained everything and requested for the letter.

After three days I received the response they confirmed that the SIM card belonged to me in the email. I took the print out of that email, ran to the police station, this time around the officer did lodge the complaint.

This time I told the officer if police can track my phone using the phone’s IMEI number, it is very much possible to be tracked, both police and service provider can track the phone by its IMEI number, anyone who is using the phone, on any call being made using any sim card from that phone can be tracked. Officer told me that only service provider or samsung can track the phone.

Next day I went to Samsung gallery, asked for the same, they said only police or service provider can track your phone, we cant help in this regard.

I once again visited the vodafone gallery, asked them if this kind of tracking is possible or not, they said we cannot track your phone only police or your handset company can track.

This is a dead end now.

Where to go, which door to knock? I will once again write an email to both Vodafone nodal officer and Samsung’s customer care.

Being a developer I know that the tracking is very much possible, but being a normal citizen who doesn’t want to bribe anyone I cant help myself.

Just wanted to share this, if anyone of you has any solution please do share.

In one of the module I came across to the situation where a physician needs to input their DEA number on a form and we have to validate the DEA Number, either using javascript or C#. I was provided with the algorithm from client and we had to write a javascript / c# function for the same. First I tried searching it on Google for some ready made stuff, but I could not found any, so written my own function to validate a DEA number. Here is the code snippet in both Javascript and C#:

C#
private bool DEAValidation(string DEANumber)
{
if (DEANumber != null && DEANumber != “” && DEANumber.Length == 9)
{
string strDEAFirstLetter = DEANumber.Substring(0, 1).ToUpper();
Response.Write(“strDEAFirstLetter: ” + strDEAFirstLetter);

if (strDEAFirstLetter != “A” && strDEAFirstLetter != “B” && strDEAFirstLetter != “M” && strDEAFirstLetter != “F”)
{
return false;
}

string strDEASecondLetter = DEANumber.Substring(1, 1).ToUpper();
string strValidChars = “ABCDEFGHIJKLMNOPQRSTUVWXYZ”;
string strValidNums = “0123456789”;
if (strValidChars.IndexOf(strDEASecondLetter) < 0 && strValidNums.IndexOf(strDEASecondLetter) < 0)
{
return false;
}

int iThirdDigit = int.Parse(DEANumber.Substring(2, 1));
int iForthDigit = int.Parse(DEANumber.Substring(3, 1));
int iFifthDigit = int.Parse(DEANumber.Substring(4, 1));
int iSixthDigit = int.Parse(DEANumber.Substring(5, 1));
int iSeventhDigit = int.Parse(DEANumber.Substring(6, 1));
int iEightDigit = int.Parse(DEANumber.Substring(7, 1));
int iNinthDigit = int.Parse(DEANumber.Substring(8, 1));

int Total_3_5_7 = iThirdDigit + iFifthDigit + iSeventhDigit;
int Total_4_6_8 = iForthDigit + iSixthDigit + iEightDigit;

int FinalTotal = Total_3_5_7 + (2 * Total_4_6_8);

Response.Write(“FinalTotal: ” + FinalTotal.ToString());

if (iNinthDigit.ToString() != FinalTotal.ToString().Substring(FinalTotal.ToString().Length – 1, 1))
{
return false;
}

return true;
}
else
{
return false;
}
}

Javascript:


function DEAValidation(DEANumber)
{
try
{
if (DEANumber != null && DEANumber != “” && DEANumber.length == 9)
{

var strDEAFirstLetter = DEANumber.substr(0, 1).toUpperCase();

if (strDEAFirstLetter != “A” && strDEAFirstLetter != “B” && strDEAFirstLetter != “M” && strDEAFirstLetter != “F”)
{
return false;
}

var strDEASecondLetter = DEANumber.substr(1, 1).toUpperCase();

var strValidChars = “ABCDEFGHIJKLMNOPQRSTUVWXYZ”;
var strValidNums = “0123456789”;

if (strValidChars.indexOf(strDEASecondLetter) < 0 && strValidNums.indexOf(strDEASecondLetter) < 0)
{
return false;
}

var iThirdDigit = parseInt(DEANumber.substr(2, 1));
var iForthDigit = parseInt(DEANumber.substr(3, 1));
var iFifthDigit = parseInt(DEANumber.substr(4, 1));
var iSixthDigit = parseInt(DEANumber.substr(5, 1));
var iSeventhDigit = parseInt(DEANumber.substr(6, 1));
var iEightDigit = parseInt(DEANumber.substr(7, 1));
var iNinthDigit = parseInt(DEANumber.substr(8, 1));

var Total_3_5_7 = iThirdDigit + iFifthDigit + iSeventhDigit;
var Total_4_6_8 = iForthDigit + iSixthDigit + iEightDigit;

var FinalTotal = Total_3_5_7 + (2 * Total_4_6_8);

if (iNinthDigit != FinalTotal.toString().substr(parseInt(FinalTotal.toString().length) – 1,1))
{
return false;
}

return true;

}
else
{
return false;
}
}
catch(err)
{
return false;
}
}