Gypsy女郎的大秘密











{十一月 27, 2007}   Javascript Hashtable

I can’t remember where did I find this so I cannot put the reference here.

function Hashtable(){
this.clear = hashtable_clear;
this.containsKey = hashtable_containsKey;
this.containsValue = hashtable_containsValue;
this.get = hashtable_get;
this.isEmpty = hashtable_isEmpty;
this.keys = hashtable_keys;
this.put = hashtable_put;
this.remove = hashtable_remove;
this.size = hashtable_size;
this.toString = hashtable_toString;
this.values = hashtable_values;
this.hashtable = new Array();
}
/*=======Private methods for internal use only========*/

function hashtable_clear(){
this.hashtable = new Array();
}

function hashtable_containsKey(key){
var exists = false;
for (var i in this.hashtable) {
if (i == key && this.hashtable[i] != null) {
exists = true;
break;
}
}
return exists;
}

function hashtable_containsValue(value){
var contains = false;
if (value != null) {
for (var i in this.hashtable) {
if (this.hashtable[i] == value) {
contains = true;
break;
}
}
}
return contains;
}

function hashtable_get(key){
return this.hashtable[key];
}

function hashtable_isEmpty(){
return (this.size == 0) ? true : false;
}

function hashtable_keys(){
var keys = new Array();
for (var i in this.hashtable) {
if (this.hashtable[i] != null)
keys.push(i);
}
return keys;
}

function hashtable_put(key, value){
if (key == null || value == null) {
throw 'NullPointerException {' + key + '},{' + value + '}';
}else{
this.hashtable[key] = value;
}
}

function hashtable_remove(key){
var rtn = this.hashtable[key];
this.hashtable[key] = null;
return rtn;
}

function hashtable_size(){
var size = 0;
for (var i in this.hashtable) {
if (this.hashtable[i] != null)
size ++;
}
return size;
}

function hashtable_toString(){
var result = '';
for (var i in this.hashtable)
{
if (this.hashtable[i] != null)
result += '{' + i + '},{' + this.hashtable[i] + '}\n';
}
return result;
}

function hashtable_values(){
var values = new Array();
for (var i in this.hashtable) {
if (this.hashtable[i] != null)
values.push(this.hashtable[i]);
}
return values;
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }



Well, I kind of know this trick for ages (really long long time ago). Actually most of the big websites now have changed their code so the annoying “Click to activate and use this control” for objects like flash does not appear anymore. All you have to do, is to have a javascript funcition for creating the object in an external js file. Use the script in your html and call the function to write the object out, like what Sony, Samsung, etc does.

Or, like my wedding website (sorry for being budget not having my own domain but rely on blogspot), have external header and footer generation functions, but leave the middle part inside the html. I can’t remember why did I do that, but I did.

Anyway a js file with a function like this and call the function in your html will do:

function CreateObject(){
document.write(‘ <OBJECT id=”WeddingHP4.swf”);
document.write(‘ codeBase=”…’);
document.write(‘ height=”470″ width=”700″ align=”middle” classid=”…” style=”border:solid 2px #FFCECC”‘);
document.write(‘ VIEWASTEXT>’);
document.write(‘ <PARAM NAME=”Movie” VALUE=”WeddingHP4.swf”‘);
document.write(‘ <PARAM NAME=”Src” VALUE=”WeddingHP4.swf”>’);
document.write(‘ <embed src=”WeddingHP4.swf” quality=”high” bgcolor=”#ffffff” width=”800″ height=”470″ name=”WeddingHP4.swf” align=”middle” allowscriptaccess=”sameDomain” type=”application/x-shockwave-flash” pluginspage=”…”> </embed>’);
document.write(‘ </OBJECT>’);
}


{八月 23, 2007}   Hide Blogger’s navbar

I am not sure would Blogger tell me to delete this post, but my other blogs have been hiding the navbar all the time. I thought of recreate my navbar and place it somewhere inside the blog instead of the top, but give up after I’ve found out it cannot search Chinese characters effectively (my other blogs are in Chinese, obviously). Anyway, here is the piece to hide the navbar:

<script type=”text/javascript”&gt;

var navBar = document.getElementById(“navbar”);
if (navBar) navBar.style.display = “none”;

</script>



{八月 23, 2007}   Simple Hover Image/Button

Back in the old days, when I have completely no idea about HTML, I found that the HoverButton created by Microsoft FrontPage was very exciting. Now hover button is nothing fancy. It’s simply using onmouseover and onmouseout events.

<input type=”image” name=”HoverButton1″ id=“HoverButton1″ onmouseover=”this.src=’/grey_btn.gif’;” onmouseout=”this.src=’/green_btn.gif’;” src=”/green_btn.gif” />

Personally I prefer wrap these up in a web control (extending ImageButton).
Download ExtendedWebControls.dll & HoverButton.cs

So in development I can add it to my toolbox and use it as a normal web control.

<%@ Register Assembly=”ExtendedWebControls” Namespace=”ExtendedWebControls” TagPrefix=”cc1″ %>

<cc1:HoverButton ID=”HoverButton1″ runat=”server” BaseImgURL=”green_btn.gif” HoverImgURL=”grey_btn.gif” />



{八月 16, 2007}   XMLHTTP readystate

Previous post has mentioned about XMLHTTP. onreadystatechange event fire up at several stages which allow response handling. Generally readystate has the following values:

0 Uninitialised before open() is called
1 Request has set up before send() is called
2 Request was sent and in process content header is available
3 In process responseText holds partial data but unfinished
4 Complete finish processing and received response

P.S. according to QuirksBlog: XMLHTTP notes: readyState and the events, different browsers behave differently for onreadystatechange event handling.



.Net AJAX extension is a easy and good tool, however there are two projects I have worked on cannot use .Net AJAX to accomplish the job (well, one was done before the AJAX 1.0 extension released). First project was in old fashion ASP. The second one is in .NET 2.0, but it has the application hold on an intra network machine, which for security reason has no internet access. However it requires a call to a web services on the client side.

Therefore javascript AJAX comes in by using XML DOM HTTP Request technique. Note that for javascript wa of AJAX, same as .NET AJAX 1.0, cannot call cross domain web services easily (.Net AJAX extension cannot call it at all without writing a proxies!). In Internet explorer, adding the external web service to the trusted site list will allow cross domain access.

Step 1: define XMLHTTP object variable

var xmlHttp;

Step 2: Create your action – in this example I will retrieve a list of book titles from the BibleWebservice provided by http://www.webservicex.net/.

function Button1_onclick() {

//Create SOAP Envelope
var xmlObj = new ActiveXObject(“Msxml2.DOMDocument”) ;
var sXml = “<?xml version=\”1.0\” ?>” ;
sXml += “<soap:Envelope “
sXml += “xmlns:xsi=\”http://www.w3.org/2001/XMLSchema-instance\”" ; sXml += “xmlns:xsd=\http://www.w3.org/2001/XMLSchema/” ;
sXml += “xmlns:soap=\”http://schemas.xmlsoap.org/soap/envelope/\”>” ;
sXml += “<soap:Body>” ;
sXml += “<GetBookTitles xmlns=\”http://www.webserviceX.NET/\”>”;
sXml += “</GetBookTitles></soap:Body></soap:Envelope>” ;
xmlObj.loadXML(sXml) ;

xmlHttp = new ActiveXObject(“Msxml2.XMLHTTP”);
// Add Post, SOAP Action and Content-Type to the request header
xmlHttp.Open (“Post”, “http://www.webservicex.net/BibleWebservice.asmx”, false) ;
xmlHttp.setRequestHeader(“SOAPAction”, “http://www.webserviceX.NET/GetBookTitles”) ;
xmlHttp.setRequestHeader(“Content-Type”, “text/xml; charset=utf-8″ ) ;
// When response received call OnRetrieveComplete method

xmlHttp.onreadystatechange = OnRetrieveComplete;
// Send request
xmlHttp.Send(xmlObj.xml) ;
}

Step 3: Create response handler

function OnRetrieveComplete(){

// readystate4 = Completed
if
(xmlHttp.readyState == 4 &&
xmlHttp.status == 200){

// Get response and perform your action
var
xmlResponse = xmlHttp.responseXML;
var bookXML = xmlResponse.selectSingleNode(“soap:Envelope/soap:Body/
GetBookTitlesResponse/GetBookTitlesResult”
).text;
var bookXMLObj = new ActiveXObject(“Msxml2.DOMDocument”) ;
bookXMLObj.loadXML(bookXML);
var bookNodes = bookXMLObj.selectNodes(“NewDataSet/Table”);

for
(i=0; i<bookNodes.length; i++)
{
… do whatever you want

}
} }



et cetera
Follow

Get every new post delivered to your Inbox.