Showing posts with label AJAX. Show all posts
Showing posts with label AJAX. 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!

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!

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!

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!