To encode Uniform Resource Identifier (URI) you can use the
encodeURIComponent()
JavaScript function like this:
javascript:location.href=’http://mysite.com/bookmark.aspx?url=’ + encodeURIComponent(location.href)+’&title=’ + encodeURIComponent(document.title)
To decode the URI you can use the decodeURIComponent JavaScript function.encodeURIComponent, JavaScript, URL Encoding
Friday, August 29, 2008
Thursday, August 21, 2008
XML response
header("Content-Type: text/xml");
$sum = $_GET["figure1"] + $_GET["figure2"];
echo <<< END_OF_FILE
END_OF_FILE
?>
The callback function now extracts the result using responseXML. It then has a first-class DOM object and can interrogate it using the standard DOM API.
var xhReq = createXMLHttpRequest();
xhReq.open("GET", "sumXML.phtml?figure1=10&figure2=20", true);
xhReq.onreadystatechange = function() {
if (xhReq.readyState != 4) { return; }
** xml = xhReq.responseXML;
var figure1 = xml.getElementsByTagName("figure")[0].firstChild.nodeValue;
var figure2 = xml.getElementsByTagName("figure")[1].firstChild.nodeValue;
var sum = xml.getElementsByTagName("outputs")[0].firstChild.nodeValue; **
...
};
xhReq.send(null);
});
The name "XMLHttpRequest" relates to its two primary functions: handling HTTP requests and converting XML responses. The former function is critical and the latter is best considered a bonus. There are certainly good applications for XML responses - see XML_Message, XML_Data_Island, and Browser-Side_XSLT - but keep in mind that XML is not a requirement of Ajax systems.
You can also upload XML. In this case, XMLHttpRequest doesn't offer any special XML functionality; you just send the XML message as you would any other message, and with an appropriate request type (e.g. "POST" or "PUT"). For the sake of the receiving web service, the Javascript should generally declare the XML content type in a request header. xhReq.setRequestHeader('Content-Type', "text/xml");
Handle Error of createXMLHttpRequest
That's great if the browser knows a problem occurred, but sometimes the request will be lost forever. Thus, you usually want some kind of timeout mechanism as well. Using Event Scheduling, establish a timer to track the session. If the request takes too long, the timer will kick in and you can then handle the error. XMLHttpRequest has an abort() function which you should also invoke in a timeout situation. Here's a code example:
var xhReq = createXMLHttpRequest();
xhReq.open("get", "infiniteLoop.phtml", true); // Server stuck in a loop.
*** var requestTimer = setTimeout(function() {
xhReq.abort();
// Handle timeout situation, e.g. Retry or inform user.
}, MAXIMUM_WAITING_TIME); ***
xhReq.onreadystatechange = function() {
if (xhReq.readyState != 4) { return; }
** clearTimeout(requestTimer); **
if (xhReq.status != 200) {
// Handle error, e.g. Display error message on page
return;
}
var serverResponse = xhReq.responseText;
...
};
A timer has been introduced. The onreadystatechange() callback function will clear the timer once it receives the full response (even if that response happens to be erroneous). In the absence of this clearance, the timer will fire, and in this case, the setTimeout sequence stipulates that abort() will be called and some recovery action can then take place.
var xhReq = createXMLHttpRequest();
xhReq.open("get", "infiniteLoop.phtml", true); // Server stuck in a loop.
*** var requestTimer = setTimeout(function() {
xhReq.abort();
// Handle timeout situation, e.g. Retry or inform user.
}, MAXIMUM_WAITING_TIME); ***
xhReq.onreadystatechange = function() {
if (xhReq.readyState != 4) { return; }
** clearTimeout(requestTimer); **
if (xhReq.status != 200) {
// Handle error, e.g. Display error message on page
return;
}
var serverResponse = xhReq.responseText;
...
};
A timer has been introduced. The onreadystatechange() callback function will clear the timer once it receives the full response (even if that response happens to be erroneous). In the absence of this clearance, the timer will fire, and in this case, the setTimeout sequence stipulates that abort() will be called and some recovery action can then take place.
Creating XMLHttpRequest Objects
Creating XMLHttpRequest Objects
In most browsers, XMLHttpRequest is a standard Javascript class, so you just create a new instance XMLHttpRequest. However, Microsoft were the inventors of XMLHttpRequest, and until IE7, IE only offered as an ActiveX object. To make things even more fun, there are different versions of that object. The following code shows a factory function that works on all browsers that support XMLHttpRequest.
function createXMLHttpRequest() {
try { return new XMLHttpRequest(); } catch(e) {}
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
alert("XMLHttpRequest not supported");
return null;
}
...
var xhReq = createXMLHttpRequest();
In most browsers, XMLHttpRequest is a standard Javascript class, so you just create a new instance XMLHttpRequest. However, Microsoft were the inventors of XMLHttpRequest, and until IE7, IE only offered as an ActiveX object. To make things even more fun, there are different versions of that object. The following code shows a factory function that works on all browsers that support XMLHttpRequest.
function createXMLHttpRequest() {
try { return new XMLHttpRequest(); } catch(e) {}
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
alert("XMLHttpRequest not supported");
return null;
}
...
var xhReq = createXMLHttpRequest();
Thursday, August 14, 2008
JavaScript trim()
String.prototype.trim = function() {
return this.replace(/^\s+\s+$/g,"");
}
String.prototype.ltrim = function() {
return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
return this.replace(/\s+$/,"");
}
// example of using trim, ltrim, and rtrim
var myString = " hello my name is ";
alert("*"+myString.trim()+"*");
alert("*"+myString.ltrim()+"*");
alert("*"+myString.rtrim()+"*");
return this.replace(/^\s+\s+$/g,"");
}
String.prototype.ltrim = function() {
return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
return this.replace(/\s+$/,"");
}
// example of using trim, ltrim, and rtrim
var myString = " hello my name is ";
alert("*"+myString.trim()+"*");
alert("*"+myString.ltrim()+"*");
alert("*"+myString.rtrim()+"*");
Tuesday, August 12, 2008
Oracle SQL get second last row record
SQL> CREATE TABLE
EMP(EMPID VARCHAR2(3),
SALARY NUMBER(8,2));
Table created.
SQL> INSERT INTO EMP
VALUES ('&A',&B);
Enter value for a: 1
Enter value for b: 70000
old 1: INSERT INTO EMP VALUES ('&A',&B)
new 1: INSERT INTO EMP VALUES ('1',70000)
1 row created.
SQL> /
Enter value for a: 2
Enter value for b: 80000
old 1: INSERT INTO EMP VALUES ('&A',&B)
new 1: INSERT INTO EMP VALUES ('2',80000)
1 row created.
SQL> /
Enter value for a: 5
Enter value for b: 56765
old 1: INSERT INTO EMP VALUES ('&A',&B)
new 1: INSERT INTO EMP VALUES ('5',56765)
1 row created.
SQL> /
Enter value for a: 4
Enter value for b: 42343
old 1: INSERT INTO EMP VALUES ('&A',&B)
new 1: INSERT INTO EMP VALUES ('4',42343)1 row created.
SQL> COMMIT;
Commit complete.
SQL> SELECT * FROM EMP;
EMP SALARY
--- ----------
1 70000
2 80000
5 56765
4 42343
[b]
(SELECT * FROM (SELECT (ROWNUM) RN, EMPID,SALARY FROM EMP)
WHERE RN >( SELECT MAX(ROWNUM)-2 FROM EMP))
MINUS
(SELECT * FROM (SELECT (ROWNUM) RN,EMPID,SALARY FROM EMP)
WHERE RN >( SELECT MAX(ROWNUM)-1 FROM EMP));
[/b]
RN EMP SALARY
---------- --- ----------
3 5 56765
EMP(EMPID VARCHAR2(3),
SALARY NUMBER(8,2));
Table created.
SQL> INSERT INTO EMP
VALUES ('&A',&B);
Enter value for a: 1
Enter value for b: 70000
old 1: INSERT INTO EMP VALUES ('&A',&B)
new 1: INSERT INTO EMP VALUES ('1',70000)
1 row created.
SQL> /
Enter value for a: 2
Enter value for b: 80000
old 1: INSERT INTO EMP VALUES ('&A',&B)
new 1: INSERT INTO EMP VALUES ('2',80000)
1 row created.
SQL> /
Enter value for a: 5
Enter value for b: 56765
old 1: INSERT INTO EMP VALUES ('&A',&B)
new 1: INSERT INTO EMP VALUES ('5',56765)
1 row created.
SQL> /
Enter value for a: 4
Enter value for b: 42343
old 1: INSERT INTO EMP VALUES ('&A',&B)
new 1: INSERT INTO EMP VALUES ('4',42343)1 row created.
SQL> COMMIT;
Commit complete.
SQL> SELECT * FROM EMP;
EMP SALARY
--- ----------
1 70000
2 80000
5 56765
4 42343
[b]
(SELECT * FROM (SELECT (ROWNUM) RN, EMPID,SALARY FROM EMP)
WHERE RN >( SELECT MAX(ROWNUM)-2 FROM EMP))
MINUS
(SELECT * FROM (SELECT (ROWNUM) RN,EMPID,SALARY FROM EMP)
WHERE RN >( SELECT MAX(ROWNUM)-1 FROM EMP));
[/b]
RN EMP SALARY
---------- --- ----------
3 5 56765
Monday, August 11, 2008
Select column name from table
To select column names of a given table you can use the following query:
Select column_name
from user_tab_cols
where table_name ='TABLE_NAME';
Remember the table name should be in capital letters.
Select column_name
from user_tab_cols
where table_name ='TABLE_NAME';
Remember the table name should be in capital letters.
Thursday, August 07, 2008
Print Error Message (exception)
Print Error Message
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);3
ex.printStackTrace(pw);
pw.close();
System.out.println(sw.toString());
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);3
ex.printStackTrace(pw);
pw.close();
System.out.println(sw.toString());
Subscribe to:
Comments (Atom)