Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts

Tuesday, November 11, 2008

Another Syntax Highlighter for Online Code Posting

Today, I found another code prettify plugin for my blog today through Google Code: SyntaxHighlighter. There is an online tutorial for how to use it in Blogger. But I did have some issues with line breaks, and Blogger add <br> to replace line breaks, which it should skip instead.

So I switch to Blog In Draft and check the option "Make Blogger in Draft my default dashboard". It solved the line break issues.


Read more!

Monday, November 3, 2008

My New Google Android G1 Phone

I just got the Android G1 phone from EBay, and the phone looks awesome. At the beginning, I opened the back cover with caution, and now I can open the back cover very quickly.

After begging for an activated TMobile SIM from one of my collegues, I was able to set up my G1 phone. After setting up the phone, I replaced the SIM with the unactivated TMobile SIM card coming with the phone, and I was able to use the Wi-Fi connection. So far, I only experienced G1 applications using Wi-Fi connections.

The browser on G1 is really amazing, and it renders a lot of pages that can't be loaded into the browser that is built into the BlackBerry.

There are several issues that concerns me:

  • I got a Bronze one and it is a little bit difficult to see the letter once the bakc light is on. So far, this is ok for me.

  • the GPS is not working very well. It is not very acurate and sometimes, it doesn't work

  • I can't manage to install G1 applications from Android Market. This really bothers me, because I am eager to try those applications.

  • ...


UPDATE: I was able to install G1 application from Android Market using Wi-Fi. I guess during the time when I tested, the network is not quite stable.

Read more!

Tuesday, October 14, 2008

Safari Fails to Include the Successful Controls in the Form Data Set

Another bug (you might need to have a Apple ID for accessing the link) for Safari!

<form action="fake_action" method="POST">
<button name="disable" value="Deactivate Account" type="submit" onclick="click();">Deactivate Account</button>
</form>

For the above form, the name/value pair for the submit button should be included in the data set when the form is submitted (i.e., the submit button is clicked). When I run the above form in Safari, the server app doesn't get
NAME         VALUE
disable   Deactivate Account

in the form data. However, if I remove the onclick event in the submit button control, the server app get the data I expect.

UPDATE:
Here is the link to the standalone test


it is a simple form with 4 different kinds of submit buttons, and here is the source code for the html page
<html>
<head>
<title>A simple form</title>
<script type='text/javascript'>
  function test(){
    document.forms[0].submit();
  }
</script>
</head>
<body>
<form action="formsubmit.php" method="POST">
<input name="disable1" value="Deactivate Account(submit button with onclick)" type="submit" onclick="test()">
<input name="disable2" value="Deactivate Account(submit button w/o onclick)" type="submit">
<button name="enable1" value="Activate Account(submit push button with onclick)" type="submit" onclick="test();">Activate Account(submit push button with onclick)</button>
<button name="enable" value="Activate Account(submit push button w/o onclick)" type="submit">Activate Account(submit push button w/o onclick)</button>
</form>
</body>
</html>


here is the source code for formsubmit.php, which simply print out the successful control sent to the server (any one of those submit buttons)
<html>
<head>
<title>Submit Form</title>
</head>
<body>
<?php

foreach ($_POST as $item) {
print $item;
}
?>
</body>
</html>


basically, I only get successful control for those submit buttons without onclick event handler, which does nothing, but call the form submit method in this test case, while I got all submit buttons in Firefox.

Read more!

Friday, September 5, 2008

What we can do with String.replace()

Here is the spec from Mozzila for String.replace().

The basic syntax for it is
var newString = str.replace(regexp/substr, newSubStr/function[, flags]);


It finds a match by either a regular expression or a string, and replaced it with the new string or the result of a function. For example,
var string = "hello world, user1 user";
var newString = string.replace("user", "thunder planet");
//the newString is "hello world, thunder planet1 user
var newString1 = string.replace(/user$/, "thunder planet")
//newString1 is "hello world, user1 thunder planet


The replacement string can include those special patterns
  • $$ -- a '$'
  • $& -- the matched substring
  • $` -- the portion of string that precedes the matched string
  • $' -- the portion of string that follows the matched string
  • $n -- if the matched string is specified using a regular expression, it gives the group in the regular expression


Please see the following examples:
var string = "hello world user"
var newString = string.replace("user", "$$");
//newString is "hello world $"
var newString1 = string.replace("user", "$&");
//newString1 is the same with string
var newString2 = string.replace("user", "$`");
//newString2 is "hello world hello world"
var newString3 = string.replace("user", "$'");
//newString3 is "hello world "
string = "Hello, John Smith";
var newString4 = string.replace(/(\w+)\s(\w+)$/g, "$2,$1")
//newString4 is "Hello, Smith,John"


The definition to the replacement function depends on whether you are using regular expression and how many groups are in the regular expression. Basically, the replacement function is in form of
function replacer(str, p1, ..., pn, offset, s)

The first parameter is the matched string, and the following n (n can be zero) parameters represent the groups in the regular expression, offset is the offset for the matched string, and the last parameter is the whole string.

function replacer(str, p1, p2, offset, s){
return "{" + str + "/" + p1 + "/" + p2 + "/" + offset + "/" + s + "}";
}

function replacer1(str, p1, offset, s){
return "{" + str + "/" + p1 + "/" + offset + "/" + s + "}";
}

"XXzzzz---Xz--XXzzz".replace(/(X+)(z+)/g, replacer)
//result is "{XXzzzz/XX/zzzz/0/XXzzzz---Xz--XXzzz}---{Xz/X/z/9/XXzzzz---Xz--XXzzz}--{XXzzz/XX/zzz/13/XXzzzz---Xz--XXzzz}"
"XXzzzz---Xz--XXzzz".replace(/(X+z+)/g, replacer)
//result is "{XXzzzz/XXzzzz/0/XXzzzz---Xz--XXzzz/undefined}---{Xz/Xz/9/XXzzzz---Xz--XXzzz/undefined}--{XXzzz/XXzzz/13/XXzzzz---Xz--XXzzz/undefined}
"XXzzzz---Xz--XXzzz".replace(/(X+z+)/g, replacer1)
//result is "{XXzzzz/XXzzzz/0/XXzzzz---Xz--XXzzz}---{Xz/Xz/9/XXzzzz---Xz--XXzzz}--{XXzzz/XXzzz/13/XXzzzz---Xz--XXzzz}"
"XXzzzz---Xz--XXzzz".replace("Xz", replacer1, "g")
//result is "X{Xz/1/XXzzzz---Xz--XXzzz/undefined}zzz---{Xz/9/XXzzzz---Xz--XXzzz/undefined}--X{Xz/14/XXzzzz---Xz--XXzzz/undefined}zz"
"XXzzzz---Xz--XXzzz".replace(/X+z+/g, replacer1)
//result is "{XXzzzz/0/XXzzzz---Xz--XXzzz/undefined}---{Xz/9/XXzzzz---Xz--XXzzz/undefined}--{XXzzz/13/XXzzzz---Xz--XXzzz/undefined}"

So the replacement function depends on the first argument in the String.replace function.

Read more!

Wednesday, September 3, 2008

Web on PSP

Here are several article discussing the web development for PSP
Web design for the Sony PSP
What Browser Does the SONY PSP Use?

The big issue with the browser on PSP is lacking support of DOM and AJAX. For example, it doesn't support innerHTML or document.createElement/document.appendChild. The only thing it supports is document.write. There is no support of XMLHttpRequest object either. It only supports flash 6, which is also disappointing.
Read more!

Wednesday, August 27, 2008

IE6 XMLHttpRequest same-origin policy

In IE6, there is no native XMLHttpRequest object, but we can use ActiveXObject('Msxml2.XMLHTTP') for the same purpose. The same-origin policy also applies to ActiveXObject in IE6, and there might be exceptions. There is a setting called "Access data sources across domains" for internet, intranet, and trusted sites zone. If it is set to enable in internet/intranet/trusted sites and the page that tries to access data in different domains falles into one of those zones, you won't get the permission denied exception. If the option is set to false, you will get the exception.
Read more!

Monday, August 25, 2008

Safari's defect on same-origin policy?

XMLHttpRequest object follows the same-origin security policy, which means you can't use it to request data from another domain. For example, the web page is hosted in domain http://www.domain.com, and it is forbidden to request data from http://www.domain1.com using XMLHttpRequest. You will get a permission denied exception when you try to call the open() method.

Same origin means same protocol, same host and same port. However, it looks like different browsers interpret this in different ways. I had an SCORM course, which is hosted in domain http://www.domain.com, but the requested app is located in domain http://www.domain.com:80. I didn't have any issue with IE and FireFox, but in Safair, I got a permission denied exception. I am wondering if this violate the same-origin policy in Safari.

Interesting discussion here for by passing the same-origin policy for XMLHttpRequest object in Firefox and IE.

PS: According to RFC 1738, the port defaults to 80 if port is ommitted. After I get rid of the 80 port number in the url for the requested app, it works in Safari. Unlike IE and Firefox, I guess Safari literally check the URL for the domain.

Read more!

Wednesday, July 2, 2008

Data Posting When the Winodw is Unloading?

I had a web application and it will do some data posting when it is closed. All the heavy lifting work in done in the onunload event handler for the page. However, it is not guaranteed that the data posting will be finished/succeed before the window is destroyed. So a Exit button is provided in the web application and the event handler to the button will do the heavy lifting work before calling
window.close();


We want to encourage users to click the Exit button instead of the browser closing button. So the design is that if users exit the application through browser closing button, a warning message will be thrown, and ask users if they really want to exit the application with the risk of losing data. If they choose ok, they will exit the application and probably the data will get lost. If they choose cancel, they will stay on the page.
window.onbeforeunload = confirmExit;

function confirmExit () {
if (!safeExit) {
return "You have attempted to leave the application without clicking the Exit button, and your progress will be lost. Are you sure you want to exit the application?";
}
}

//event handler for the Exit button
function doClickExit(){
safeExit = true;
//blahblah
}

Please see my previous post for more details about setting up the onbeforeunload event handler.

PS: I also tried synchronous XMLHttpRequest object for posting data, and it looks like it has a higher rate (maybe there is no data get lost forever) of posting data in the onunload event handler. But still, it is a good practice to provide a way which guarantees that data will be persisted and users are aware of it.

Read more!

Thursday, June 26, 2008

Use xmlHTTPRequest for Accessing Local Files

here are some of my experiences working with xmlHTTPRequest object to open local files.
xhr.open("GET",localFile,false);          
httpRequest.onreadystatechange = function(){
if (xhr.readyState == 4) {
if (xhr.status == 200) {
//blahblah...
} else {
//blahblah...
}
}
xhr.send(null);


First of all, there will be exceptions if you trying to access a local file that doesn't exist. Instead of returning 404 status code, it throws out an exception. In addition, the status code from opening local files will be 0. Below is quote from Mozilla website for xmlHTTPRequest object.

file:/// and ftp:// do not return HTTP status, which is why they return zero for status and an empty string for statusText. please refer to bug 331610 for more insight.

I didn't find any IE related document to xmlHTTPRequest object, but from my test, it also return 0 for file:///.

Below is the modification to original code
try {
xhr.open("GET",localFile,false);
httpRequest.onreadystatechange = function(){
if (xhr.readyState == 4) {
if (xhr.status < 300) {
//blahblah...
} else {
//blahblah...
}
}
}
xhr.send(null);
} catch (ex) {
//blahblah...
}


Read more!

Wednesday, June 25, 2008

FireFox 3 is out

Good news that FireFox 3 is out, and I think it cheers a lot of geeks. But I will still keep using FireFox 2 for a while until FireBug is integrated with FireFox 3 smoothly. During the FireFox 3 beta testing, a lot of people has found a lot of issues using FireBug. In addition, Mozilla has reported a minor vulnerability in FireFox 3 and its earlier version.
Read more!

Does FireFox support beforeunload event?

The answer is yes. FireFox does support beforeunload event. However, you can't expect it to work if you put its event handler into the <body> tag as we do in IE.
In IE, there are several ways to define the onbeforeunload event handler


(1)<body onbeforeunload='confirmExit();'>

(2)document.body.onbeforeunload=confirmExit;

(3)window.onbeforeunload=confirmExit;

function confirmExit(){
return "You are about to leave this page. Are you sure?";
}

in FireFox, you can only use the (3) to trigger the unbeforeunload event handler. It frustrated me first, because I thought they are all same, but why I didn't get what I expected. I guess it is not supported very well and the first two approaches don't work in FireFox. So if you want to use onbeforeunload, please use the (3) to make it work on both IE and FireFox.

Read more!

Tuesday, May 6, 2008

getURL and onBeforeUnload

Any getURL calls in the flash module will trigger the onBeforeUnload event in IE6. So if you have some critical logic that is attached to onBeforeUnload, you should be aware about this issue. For example, if you try to start the timer in the load event and trying to stop the timer in beforeUnload event, calling getURL in the flash module may cause unexpected behavior in IE6. This is not a case in FireFox 2. I didn't know if this is still an issue in IE7.
Read more!