Thursday 31 March 2011

Hard reboot and Soft reboot

A hard reboot (also known as a cold reboot, cold boot or cold start) is when power to a computer is abruptly turned off, then turned back on. This starts the computer without first performing any shut-down procedure. With many operating systems, especially those using disk caches, after a hard reboot the filesystem may be in an "unclean" state, and an automatic scan of on-disk filesystem structures will be done before normal operation can begin. It may be caused by power failure, be done by accident, or be done deliberately as a last resort to forcibly retrieve the system from instances of a system freeze, critical error or virus-inflicted DoS attack. It can also be used by intruders to access cryptographic keys from RAM, in which case it is called a cold boot attack. The attack relies on the data remanence property of DRAM and SRAM to retrieve memory contents which remain readable in the seconds to minutes after power has been removed.


A soft reboot (also known as a warm reboot) is restarting a computer under software control, without removing power or (directly) triggering a reset line. It usually refers to an orderly shutdown and restarting of the machine.

Thursday 17 March 2011

JavaScript replace() Method

The replace() method searches for a match between a substring (or regular expression) and a string, and replaces the matched substring with a new substring

Syntax

string.replace(regexp/substr,newstring)

Both are required arguments.

To replace all matches with the newstring it is better to use the regular expression version of it. string.replace(substr,newstring) replaces only the first match. But string.replace(/regexp/g,newstring) replaces all matches with newstring. Also you can do case-insensitive replace using regexp i.e. string.replace(/regexp/gi,newstring)

<script type="text/javascript">

var str="I enjoy scripting"; //Not really!!!

//Following script will do a case-insensitive search for the string "enjoy" in the var str and will replace all occurances with "hate"

document.write(str.replace(/enjoy/gi, "hate"));

//And the output will be "I hate coding"

//Following script will do a global search for spaces in the var str and replaces them with non-breaking spaces.

document.write(str.replace(/ /g, "&nbsp;"));


//The output will be "I&nbsp;enjoy&nbsp;coding"

</script>

Non-breaking Space in HTML

&nbsp; is the entity used to represent a non-breaking space. It is essentially a standard space, the primary difference is a browser will not not break (or wrap) a line of text at the point that this &nbsp;occupies.

You can use the non-breaking space to indent text and images a small amount. It's not a good idea to use a lot in a row, as older browsers don't support more than one at a time.

The Single Pixel GIF Trick

This uses a transparent GIF image that is 1 pixel square. You take that image and stretch it out to force your HTML to lay the way you want it to. For example:

<p><img src="spacer.gif" alt=" " width="1" height="1" style="margin-right: 5px; margin-left: 5px;" />This paragraph is indented 11 pixels.</p>

Monday 14 March 2011

Serialize and Deserialize using JavaScriptSerializer and JSON

Ever worndered how to serialize your object instance into a JSON string. You can use System.Web.Script.Serialization.JavaScriptSerializer to do this.

public static string SerializeToJSON(T obj)
{
System.Web.Script.Serialization.JavaScriptSerializer scriptSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();

StringBuilder jsonBuilder = new StringBuilder();

scriptSerializer.Serialize(obj, jsonBuilder);

return jsonBuilder.ToString();
}

And it is quite straightforward to deserialise the JSON string into the original object instance

OriginalType obj = (new System.Web.Script.Serialization.JavaScriptSerializer()).Deserialize(JSONString);

Anonymous C# method for XML serialization

Following is an anonymous C# method which can be used to serialize any given object into XML.

public static string SerializeToXML(T obj)
{
string serializedContext;

System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());

StringWriter stwOutPut = new StringWriter(new StringBuilder());

serializer.Serialize(stwOutPut, obj);

serializedContext = stwOutPut.ToString();

//Replace tag
serializedContext = serializedContext.Replace("", " ");

serializedContext = serializedContext.Replace("\r\n", " ");

return serializedContext;
}

How to disable radiobutton list via jQuery?

Using normal JavaScript you have to find all the elements in the radio group, which can be done using document.getElementsByTagName(radiogroupname), and iterate through the list to do this. But it is very simple to do this using jQuery, just one line of code.

Use either

$("*[name$='radiogroupname']").attr("disabled", "true");

or

$("input:radio[name$='radiogroupname']").attr("disabled", "true");

It will disable all the radio buttons in the group.

:) Happy scripting