Search

8/30/2007

get event target

這個很常用,記錄一下,就是我們想抓取事件發生的DOM node的reference,譬如我要點一個input text,然後清空,必須先找到該input text的reference,以下兩各版本,第一個是from scratch,第二個是prototype version.

example html

<body id="b">
<div id="t"> test </div>
<div id="a"> another test</div>
</body>

1. conventional version
  document.body.onclick = function(event){
var e = event||window.event;
var t = e.target||e.srcElement;
alert(t.innerHTML);
}

2. prototype version
Event.observe('b', 'click', function(e){ alert(Event.element(e).innerHTML) })

8/24/2007

Fiddler

Fiddler

Fiddler is a HTTP Debugging Proxy which logs all HTTP traffic between your computer and the Internet. Fiddler allows you to inspect all HTTP Traffic, set breakpoints, and "fiddle" with incoming or outgoing data. Fiddler includes a powerful event-based scripting subsystem, and can be extended using any .NET language.

8/22/2007

dynamic loading JavaScript

石頭閒語:Load and Execute JavaScript on Demand, by createElement
石頭閒語:Load and Execute JavaScript on Demand
最近碰到一個問題,需要動態的載入外部的JavaScript,因為是外部的domain,所以不能用XMLHttpRequest來載入,需要用document.createElement(script)來載入,使用這個方法有一個問題就是由於所有的script是同時的loading,會遇到還沒loading完的function執行會發生問題。解法簡單的說就是callee必須要改變一個global variable, caller必須每隔幾秒去檢查這個global variable,如果被改掉了,就是loading好了。

下面是會有問題的例子。
test.html

<html>
<head>
<script type="text/javascript" src="general.js"></script>
</head>
</html>

general.js
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'snip.js';
document.getElementsByTagName('head')[0].appendChild(script);
getSomething();

snip.js
function getSomething(){
alert('something');
}
alert('hello');

下面是按照石頭成改出來會work的例子:
general.js
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'snip.js';
document.getElementsByTagName('head')[0].appendChild(script);
window.loadingScript = 1;
var interval = setInterval(
function(){
if(window.loadingScript==0){
clearInterval(interval);
getSomething();
}
}, 1000
)

snip.js

window.loadingScripts--;
function getSomething(){
alert('something');
}

__defineGetter__ and __defineSetter__

http://webfx.eae.net/dhtml/ieemu/eventobject.html

The problem shows up when you try to get the target when you click on a link with some text in it. IE (using srcElement) returns the link but Mozilla returns the text node (using target) inside the link.

Source Element
Event.prototype.__defineGetter__("srcElement", function () {
var node = this.target;
while (node.nodeType != 1) node = node.parentNode;
return node;
}

Cancel the event bubble
Event.prototype.__defineSetter__("cancelBubble", function (b) {
if (b) this.stopPropagation();
});

Preventing default actions
Event.prototype.__defineSetter__("returnValue", function (b) {
if (!b) this.preventDefault();
});

offsetX and offsetY
Event.prototype.__defineGetter__("offsetX", function () {
return this.layerX;
});

Event.prototype.__defineGetter__("offsetY", function () {
return this.layerY;
});

8/20/2007

ColorZilla Extension for Firefox and Mozilla

ColorZilla Extension for Firefox and Mozilla - 可以吸取browser某一個pixel的顏色,找很久了。

programming interview resources

1. Wrox.Programming.Interviews.Exposed.2nd.Edition
- 這本書詳細列出常考的題目,依照類型分類(Linked lists, Trees & graphs, recursion, ...)還包括一些resume寫作上的建議。

2. The ultimate list of questions and answers to help prepare for your interview!

3. http://del.icio.us/chenyang/job - 這邊蒐集了不少網路上的題庫。

4. 其他一些open question(ex: M&M怎麼做的,全美國有幾座加油站,...)在如何移動富士山有討論

5. Microsoft Interview Questions

What's the difference between a linked list and an array?
Implement a linked list. Why did you pick the method you did?
Implement an algorithm to sort a linked list. Why did you pick the method you did? Now do it in O(n) time.
Describe advantages and disadvantages of the various stock sorting algorithms.
Implement an algorithm to reverse a linked list. Now do it without recursion.
Implement an algorithm to insert a node into a circular linked list without traversing it.
Implement an algorithm to sort an array. Why did you pick the method you did?
Implement an algorithm to do wild card string matching.
Implement strstr() (or some other string library function).
Reverse a string. Optimize for speed. Optimize for space.
Reverse the words in a sentence, i.e. "My name is Chris" becomes "Chris is name My." Optimize for speed. Optimize for space.
Find a substring. Optimize for speed. Optimize for space.
Compare two strings using O(n) time with constant space.
Suppose you have an array of 1001 integers. The integers are in random order, but you know each of the integers is between 1 and 1000 (inclusive). In addition, each number appears only once in the array, except for one number, which occurs twice. Assume that you can access each element of the array only once. Describe an algorithm to find the repeated number. If you used auxiliary storage in your algorithm, can you find an algorithm that does not require it?
Count the number of set bits in a number. Now optimize for speed. Now optimize for size.
Multiple by 8 without using multiplication or addition. Now do the same with 7.
Add numbers in base n (not any of the popular ones like 10, 16, 8 or 2 -- I hear that Charles Simonyi, the inventor of Hungarian Notation, favors -2 when asking this question).
Write routines to read and write a bounded buffer.
Write routines to manage a heap using an existing array.
Implement an algorithm to take an array and return one with only unique elements in it.
Implement an algorithm that takes two strings as input, and returns the intersection of the two, with each letter represented at most once. Now speed it up. Now test it.
Implement an algorithm to print out all files below a given root node.
Given that you are receiving samples from an instrument at a constant rate, and you have constant storage space, how would you design a storage algorithm that would allow me to get a representative readout of data, no matter when I looked at it? In other words, representative of the behavior of the system to date.
How would you find a cycle in a linked list?
Give me an algorithm to shuffle a deck of cards, given that the cards are stored in an array of ints.
The following asm block performs a common math function, what is it?
cwd xor ax, dx
sub ax, dx
Imagine this scenario:
I/O completion ports are communictaions ports which take handles to files, sockets, or any other I/O. When a Read or Write is submitted to them, they cache the data (if necessary), and attempt to take the request to completion. Upon error or completion, they call a user-supplied function to let the users application know that that particular request has completed. They work asynchronously, and can process an unlimited number of simultaneous requests.
Design the implementation and thread models for I/O completion ports. Remember to take into account multi-processor machines.
Write a function that takes in a string parameter and checks to see whether or not it is an integer, and if it is then return the integer value.
Write a function to print all of the permutations of a string.
Implement malloc.
Write a function to print the Fibonacci numbers.
Write a function to copy two strings, A and B. The last few bytes of string A overlap the first few bytes of string B.
How would you write qsort?
How would you print out the data in a binary tree, level by level, starting at the top?


6. 很常考quicksort,包括implement, best/worst case發生的情況。

8/19/2007

phone interview tips

Phone Interview Tips

  • Write out your 4 top accomplishments

  • Write out quality questions - ask these throughout the interview---allows the interview to be more conversational instead of an interrogation. Also shows level of intelligence, interest in the position, and the homework you’ve done.

  • Write out your answer to the “tell me about yourself” question

    1. Prepare 1 sentence summary of your career history—not easy but it can be done.
    2. Prepare 1-2 sentence summary of a single accomplishment that you are proud of that will also capture the potential employer’s attention.

    3. Prepare 1 sentence summary of specifically what you want to do next in your career- this will be company-specific and therefore unique based upon the employer’s needs

  • Review your resume thoroughly - anything listed is fair game.
  • Prepare for the “typical interview questions”

Remember, this isn’t the appropriate time to talk money. Should they ask your expectations, you can certainly say you’re interested in learning more about the opportunity and you’d consider a fair and reasonable offer. DO NOT GIVE OUT A NUMBER when asked about expectations. This will hurt your negotiation later on. You can, however, give your current base salary if they ask as that will need to be disclosed at some point.

Be enthusiastic, motivated, and genuinely interested in learning more about the position. The goal is to get an invitation to visit their office.

8/15/2007

reCAHTCHA

http://recaptcha.net/learnmore.html - 之前一直不知道reCAHTCHA的原理,簡單的說,分成兩個部分,第一個部分是知道正確答案的word(s),用來確認,第二部分是書 scan 出來的圖,統計最多人的就當作正確答案。不過如果知道上下文的話,人的辨視度會高很多。

http://recaptcha.net/learnmore.html
But if a computer can't read such a CAPTCHA, how does the system know the correct answer to the puzzle? Here's how: Each new word that cannot be read correctly by OCR is given to a user in conjunction with another word for which the answer is already known. The user is then asked to read both words. If they solve the one for which the answer is known, the system assumes their answer is correct for the new one. The system then gives the new image to a number of other people to determine, with higher confidence, whether the original answer was correct.

8/08/2007

本月閱讀2007.08

台灣學生素質很差 (?) « Pyridine
台灣的教授素質很差 (?) « Pyridine

美國大學教育最大的 paradox, 就是: 為什麼美國大學生素質那麼差, 不過他們卻是全世界高等教育最先進的國家? 原因很多, 其中之一, 跟老師有關. 美國研究型大學教授發表論文的壓力高, 其實不在意教學. 就算是在最好的學校, 大學生也不見得可以得到什麼特別好的教育. 不過差別在於他們的教授真的是一流的人才, 儘管那些教授教書教得不好, 他們卻在言行之間流露出了一流人才的水準. 有心的學生因此對自己有很高的期望. 水漲船高, 正是提高學生素質的關鍵.

Cornelius' Blog: Vim 學習地圖
YUI 2.3.0: Six New Components and a Prettier Face » Yahoo! User Interface Blog
Particletree » Loading Content with JSON
程式者的胡言亂語 : 一個程式者的胡言亂語
破碎中國:Come Fly With Me...
石頭閒語:關於 Closure 和 Anonymous function 的差別 - 樂多日誌
MF Bliki: Closure
sizeof(char) is 1 « code monk
慧劍禪心’s Live 紀錄
教學:透過screen + irssi使用IRC
樂活族 - Wikipedia
樂活族又稱樂活生活、洛哈思主義、樂活,是一個西方傳來的新興生活型態族群, 由音譯LOHAS而來, LOHAS是英語Lifestyles of Health and Sustainability的縮寫,意為以健康及自給自足的型態過生活。
此名詞最早出現在1998年的書籍《文化創造:5000萬人如何改變世界》(The Cultural Creatives: How 50 Million People are Changing the World),作者是美國社會學家雷·保羅(Paul Ray) ,書中對LOHAS族的定義為「一群人在做消費決策時,會考慮到自己與家人的健康和環境責任。」據估計這一族群的市場,在美國就達到有2千2百89億美金。樂活族的特色是身體力行他們關心的環保議題,除自己消費對健康有益, 不會汙染環境的商品,也鼓勵大家改變消費態度。在做消費決策時,會考慮到自己與家人的健康和環境責任。