Sunday, February 28, 2010

Replace all string values from a variable in a loop using JavaScript - Sample code

If you have looked at my other article, you already know how to replace all the matching string from a string variable. However, some times you will need to replace all the string values that are received from a variable.

This can be achieved by creating a Regular Expression object with the necessary variables and passing it to the regular replace method of the string. Below is the sample code.

var strToBeReplaced = "This 1 is a warning 2";
for (var k=1;k<3;k++)
{
    strToBeReplaced = strToBeReplaced.replace(new RegExp(k, 'g'), "message");
}
alert(strToBeReplaced);
//shows "This message is a warning message"


Wednesday, February 24, 2010

Convert or Replace carriage-return/New-Line/line-break/End-of-line characters in JavaScript

Even though this might seem simple, you might come across cross browser issues and other issues based on the pre-processing of the string from different programming languages. Let's forget all those and keep the below concepts in mind.

From JavaScript's perspective, the string can have "\r\n" as a whole OR "\r" only OR "\n" only. In most of the scenario, the string variable will be having \n at a minimum. Thus, considering all three possibilities, what we can do is this.

var newString = oldString.replace(/\n/g, "< br >").replace(/\r/g, "");

Note that the above sample code is using a Regular Expression match using "g", so that all the occurences will get replaced. Now, you can change the above logic as per your needs.

More Information:
For those who tend to confuse with all these characters, here is what it is in a nutshell. As an example, this is what gets stored in the variable when user press [ENTER] in the keyboard. The following are their representation as special characters.

\n New Line/Line feed
\r Carriage Return

Generally, UNIX systems will have "\n", "\r" will be used in MAC and a comibnation of "\r\n" will be used in Windows OS.

JavaScript replaceAll - replace all occurrences of a string with new string

During string replace, JavaScript replace() function replaces only the first occurence of the match. Use Regular Expression match to replace all the occurrences of the specified string. Regular Expression match can be specified by providing a "g" next to it, as shown below.

var stringVariable = "This message is a warning message";
alert(stringVariable.replace("message", "alert"));
//shows "This alert is a warning message"

alert(stringVariable.replace(/message/g, "alert"));
//shows "This alert is a warning alert"


Friday, February 19, 2010

LIC premium will not be payable using BillDesk through SBIcard.com

I had a BillDesk account which was accessible through sbicard.com. One of my LIC policy was added on that account. The payment notification through email and SMS were working fine.

When i went to add one more policy, i was unable to find LIC listed to select. Upon contacting BillDesk, they said that the option to pay for LIC has been removed and they will not processing the payments for the already existing LIC policies too. Unlucky me!

Not sure whether this is applicable all over India.

Thursday, February 18, 2010

ASP.net menu control does not render properly in Chrome and Safari

This was one major problem in the ASP.net website that I was working on. The control rendered properly in IE, Firefox and Opera EXCEPT Chrome and Safari. The initial guess was that there is something going wrong with my doctype settings and the CSS styles being applied.

A quick search over the net revealed that changing the Page.ClientTarget to "uplevel" will solve the issue; and it worked for few. But this didn't work for me.

Then there was a solution changing the web.config for adding browserCaps in the system.web section, which also failed for me.

Then, at last, there was solution adding .browser file to App_Browsers folder which worked for me.

Here are the steps.
1. Right click your project folder to add an ASP.net folder "App_Browsers".
2. Click "Add New" and create a browser file named Chrome.browser
3. Put the following code in that file after commenting the existing code.

<browsers>
<browser refID="safari1plus">
<controlAdapters>
<adapter controlType="System.Web.UI.WebControls.Menu" adapterType="" />
</controlAdapters>
</browser>
</browsers>

References:
http://www.google.com/support/forum/p/Chrome/thread?tid=7506cca26ec06af7&amp;hl=en
http://forums.asp.net/p/1369765/2860698.aspx#2860698
http://browsercompatibility.codeplex.com/



CSS background image URL setting not working for table cell in Safari and Chrome

I was scratching my head over this one. The inline style was specified as
style="background-image:url('image_link.jpg');
and it was working in all browsers like Internet Explorer, Firefox and Opera EXCEPT safari and chrome. After some trial and error, i found that this was due to the fact that my table cell (TD) was not having any contents. I inserted an non-breaking space and Voila! It works now.