Wednesday, March 17, 2010

Best Way to Round Up Numbers Using Javascript

I previously found several solutions to round up (float) numbers to closest two decimal places. Some are using complicated function with sofisticated codes but I find this one-liner quite effective and efficient:Math.round(num*100)/100; // for two decimal placesMath.round(num*1000)/1000; // for three decimal places..I appreciate the author who did this and sorry for forgetting where I get it fr...
Read More »

Tuesday, March 16, 2010

Firefox isn’t Foxy When it comes to Javascript Execution

Strangely, Firefox executes Javascripts the slowest compared to IE, Safari and Chrome. It is a strange phenomenon as it doesn’t affect IE, Safari or Chrome. There is also a claim that Firefox 3.5 or higher can run Javascript faster. Since there are still many who are using Firefox 3.0 or lower, I will stick to it unless 3.0 has been forced to phase off.No real fix can be found yet unless to upgrade to version 3.5 or higher which is not a good solution. I cannot just ask all the visitors...
Read More »

Monday, March 15, 2010

Onmouseover/Onmouseout Event Trap Trick

If you happen to need to do a mouse trap on certain link in HTML, you will use onmouseover and onmouseout to due with it. There is one way to save the onmouseout event trap to simplify the procedure. It is to define a fixed property to it such as the color of a table, the color of a border or even a background image. In that case, during a onmouseout event, the browser will go back to your original fixed settings. But if you purposely define how to due with it during a onmouseout event,...
Read More »

Sunday, March 14, 2010

Firefox Pointer Change Problem Using Javascript in <MAP>

If you are using <MAP> and you want to do a onmouseover event trap to change the mouse cursor to ‘pointer’, it won’t work in Firefox but still good for the rest, viz., IE, Chrome and Safari. I didn’t it on Opera though.Firefox requires at least a hyperlink anchor, viz., <a href=…> to change the cursor to pointer whenever the mouse it on top of the defined area.Good news is that this is the workaround. Bad news is that for firefox, you can only change it to ‘pointer’ type cursor...
Read More »

Saturday, March 13, 2010

MySQL Problem with Japanese/UTF-8 texts

If you have tried to put Japanese or UTF-8 texts in MySQL, chances are it will show “junky” characters after you save/reopen it. An adjustment is needed to fix this simple problem:Declare the data type as VarBinary. Problem solved.I’ve tried changing the character set to UTF-8, using the “mysql_enable_utf8=1” setting, making SQL query like “SET CHARACTER SET utf8_unicode_ci;”, nothing seems working. Anyway the simple solution above solved the problem. I have tried it with MySQL 5.x.Pitfall:...
Read More »

Friday, March 12, 2010

IE Text <INPUT> Minimum Width Problem

If you happen to specify the “size” in the text input form using <input> which is less than 10, chances are the size will be “fixed” to around 10 even if you specify a lower number. In order to solve this peculiar problem of IE or maybe Firefox or Chrome, I have tried the following CSS declaration and it works:table-layout: fixed;Strangely, only Safari works well without this fix. Firefox and Chrome can’t even work well using this fix. But at least IE doesn’t have this problem after...
Read More »

Thursday, March 11, 2010

Paypal Login Window Set to Other Languages (Japanese) by Default

I was requested by a Japanese customer to set the default login window in Japanese. I have tried setting the lc (locale) query string to “JP”, it just works half way with English and Japanese as the choices. But Japanese was treated as second language in the locale of “JP”. The default page is still English. Then I tried all the funny things and eventually this works out right:<input type=hidden name="locale.x" value="ja_JP">Now the default Paypal login page is in Japanese! But of...
Read More »

Wednesday, March 10, 2010

Fixing PERL script saved in UTF-8 Format using Notepad

If your PERL script needs to display non-English texts, say Japanese or Chinese, usually you will save your script in UTF-8 format. Problem arises if you are using Notepad to save the UTF-8 format as it adds the BOM (Byte Order Mark) header to the file.The problem is that PERL won’t run the UTF-8 file with BOM header once it is uploaded to the server. However, it will run fine if you are running with later version of Apache locally using your Windows based PC.To solve this problem (if...
Read More »

Tuesday, March 9, 2010

Redirect a URL inside a frame to a _top page

It’s easy to redirect a URL if you don’t care whether you are from inside a frame or to its own frame using META redirection or window.location javascript redirection.What if your HTML is inside a frame calling to redirect itself to a _top page? Tricky?I use this:if (document.referrer.indexOf('YourAliasName') != -1) {   top.location.href='http://www.YourMainDomainName.com';}You can substitute YourAliasName with a portion of your domain alias name so that the redirection...
Read More »

Monday, March 8, 2010

Use Javascipt to change the <select> Pre-select Item

I use this to change:document.getElementById('idname').selectedIndex = 0;Tested to work fine with latest IE, Firefox, Safari and Chrome. Your <select> also needs to address the id name such as: <select id=idname>You can change the selectedIndex to something else. 0 means the first selection, 1 means the second, e...
Read More »

Sunday, March 7, 2010

Assigning SetTimeout/SetInterval Functions with Argument(s)

If Javascipt’s SetTimeout()/SetInterval() is assigned with a function that contains argument, it won’t run. After trials and errors, here’s the way that works well with latest version of IE, Firefox, Safari and Chrome:setInterval(function(){function_name(argument)}, 1000);Noted those highlighted in red is a function to call another function. I have tried using eval(), it works well in Firefox but it won’t in IE. This trick is to make sure it works with all type of browsers. Using common...
Read More »

Saturday, March 6, 2010

Calling a Function in iframe

I mentioned how I call a function from iframe. Now I explain how I do the opposite, calling a function in iframe from the parent or main HTML.After trying a few suggestions found using Google, I find this working for all the browers I use, viz., IE, Firefox, Safari and Chrome:document.getElementById('framename').contentWindow.functioname(argument);You can replace your own framename, functionname and argument.Update: It will not work on Chrome if you are running in local host due to security...
Read More »

Friday, March 5, 2010

Paypal doesn’t like to be framed in IE

I tried to redirect a shopping cart form to an iframe a while ago but the Paypal keeps showing an Error something like "your last action could not be completed" while using IE but it works fine in Firefox, Safari and Chrome.Then I make the redirection to a brand new page by adding the "target = _top" to the link anchor and everything works fine now.Weird problem. Obviously Paypal doesn't like us to enclose it in a frame.September 2011 Update: Now Paypal doesn't allow its content to be...
Read More »

Thursday, March 4, 2010

Speeding up Firefox

I notice HTML content will be a bit slow to show up on Firefox whenever there is an error reported in Firefox Error Console.As a responsible web developer, it is our duty to run the Error Console every now and then. Just clear the history and have a run of Error Console and reload of target website, all the problems with the Firefox will show up. Fixing some problems in Firefox platform could fix the speed problem in other browsers as well. So start screening with Firefox’s Error Console...
Read More »

Wednesday, March 3, 2010

Firefox is responding slow on Javascript’s onresize()

Compared to IE, Safari and Chrome, Firefox is the slowest in response to the onresize usually put in the <body> tag.I was testing this using the latest Firefox version and am not sure how the older versions did. I make my HTML to call a function to relocate an object whenever there is a resize event on the browser’s window. Something interesting for developers to pay attention to. If your onresize() is responding slowly to resize events, chances are you are using Firefox 3 and above....
Read More »

Tuesday, March 2, 2010

Diplaying Server Date Using PERL and Javascript

I only find PHP solution after googling around, but I wanted to use PERL. Here’s how I did it:PERL side:#!/usr/bin/perl$time = localtime(time);print "Content-type: text/html\n\n";print "var serverdate='$time';"; exit; Javascript side:<script language="JavaScript" src="/cgi-bin/getservertime"></script><script type="text/javascript"><!--document.getElementById('time').innerHTML = '<font face=tahoma size=1>'+serverdate+'</font>'; //--></script>Please...
Read More »

Monday, March 1, 2010

Get the same Browser’s width value across all Browser types

Firefox, Safari and Chrome have almost the same way to measure the page width of the browser window except IE.This is due to the fact that IE is using different way to get the width of the window than Firefox/Safari/Chrome.IE uses “window.innerWidth” property whereas Firefox/Safari/Chrome uses “document.documentElement.clientWidth” to get the width of the browser.The problem now is that IE tends to include the width of the scrollbar where around 15-18 pixels are added to the sum.I use...
Read More »