Search

12/04/2007

clientX / clientY / pageX / pageY

對一個 event 而言,我們常會用到的是 event 的(1)距離 document 左邊距離多少 / (2)距離 document 上面距離多少。
W3C Definition

clientX of type long, readonly
The horizontal coordinate at which the event occurred relative to the DOM implementation's client area.
clientY of type long, readonly
The vertical coordinate at which the event occurred relative to the DOM implementation's client area.

MDC Definition
event.clientX: Returns the horizontal coordinate within the application's client area at which the event occurred (as opposed to the coordinates within the page).
event.pageX: Returns the horizontal coordinate of the event relative to whole document.

DOM標準中,未明確定義 clientX 與 clientY 是相對於 canvas 還是 document,Firefox的作法是以 canvas 為準,並且另外定義以 document為準的 pageX 與 pageY。IE的 implement 是以 canvas 為準,所以需要加上捲軸的高度。其他的瀏覽器(Opera Konqueror iCab)


所以
by document: Mozilla Firefox (pageX/pageY),Opera, Konqueror, iCab
by canvas: Mozilla Firefox (clientX/clientY),IE


var ex, ey;
if(e.pageX && e.pageY){ // Mozilla Firefox
ex = e.pageX;
ey = e.pageY;
}
else if(e.clientx && e.clientY){
ex = e.clientx;
ey = e.clientY;
if(isIE){ // add scrollLeft, scrollTop to it.
ex += document.body.scrollLeft;
ey += document.body.scrollTop;
}
}

ref: PPK - JavaScript - Find position

offsetHeight / offsetWidth / offsetTop / offsetLeft

對一個 element 而言,我們常會用到的是 element 的(1)寬 / (2)高 / 與 (3)document 左邊距離多少 / (4)與 document 上面距離多少。
(1) elemenet 的寬就是 element.offsetWidth
(2) elemenet 的高就是 element.offsetHeight
(3) offsetTop 是 element 與 offsetParent 的距離。所以(3)的算法就是一直累加 offsetParent 的 offsetLeft,code 如下:

function findPosX(obj){
var curLeft = 0;
do{
curLeft += obj.offsetLeft;
} while(obj = obj.offsetParent);
}
return curLeft;

(4) 同理 offsetLeft 的算法為
function findPosX(obj){
var curTop = 0;
do{
curTop += obj.offsetTop;
} while(obj = obj.offsetParent);
}
return curTop;

12/03/2007

John Resig - Easy PDF Sharing

John Resig - Easy PDF Sharing - 用 Image Magic 把 pdf 轉成 png 在輸出在頁面上 - cool。下面中的 comment 有 slideshare 的人提到為什麼他們選擇用 swf 而不是 jpg/png 是因為 scaling 的問題。

SWFs scale very neatly and we can have players of different sizes from embeds to full screen.

source code

Better ways to writing JavaScript

Better ways to writing JavaScript

function get(el) {
return document.getElementById(el);
}
//usage:
$('foo', 'bar', 'baz');

var addEvent = function() {
if (window.addEventListener) {
return function(el, type, fn) {
el.addEventListener(type, fn, false);
};
} else if (window.attachEvent) {
return function(el, type, fn) {
var f = function() {
fn.call(el, window.event);
};
el.attachEvent('on' + type, f);
};
}
}();
//usage:
addEvent(get('example'), 'click', function(e) {
alert('hello world');
});

var asyncRequest = function() {
function handleReadyState(o, callback) {
if (o && o.readyState == 4 && o.status == 200) {
if (callback) {
callback(o);
}
}
}
var getXHR = function() {
var http;
try {
http = new XMLHttpRequest;
getXHR = function() {
return new XMLHttpRequest;
};
}
catch(e) {
var msxml = [
‘MSXML2.XMLHTTP.3.0′,
‘MSXML2.XMLHTTP’,
‘Microsoft.XMLHTTP’
];
for (var i=0, len = msxml.length; i < len; ++i) {
try {
http = new ActiveXObject(msxml[i]);
getXHR = function() {
return new ActiveXObject(msxml[i]);
};
break;
}
catch(e) {}
}
}
return http;
};
return function(method, uri, callback, postData) {
var http = getXHR();
http.open(method, uri, true);
handleReadyState(http, callback);
http.send(postData || null);
return http;
};
}();
usage:
asyncRequest('GET', 'test.php?foo=bar', callback);
function callback(o) {
alert(o.responseText);
}

editCSS for Internet Explorer

crisp's blog » Blog Archive » editCSS for Internet Explorer - concept - 底下的comment有提到accessibility toolbar,在CSS->Test Styles->選一個element,點edit style rule。
sourcecode

Now I was finally in business and so I present to you a simple concept of an editCSS feature in Internet Explorer (this link will only work in IE ofcourse…)

Not to mention that this is only a concept and has a lot of limitations: it doesn't work for stylesheets on another domain (since it uses XMLHttpRequest and thus is origin-bound), it doesn't really take into account media-types, the rules for alternate stylesheets are not really respected, it doesn't allow you to modify inline style-elements, it doesn't take into account @import or @media rules, it's a popup instead of a sidebar etcetera etcetera.

YUI Dimension api

getViewportHeight
Int getViewportHeight ( )
Returns the current height of the viewport.

Returns: Int
The height of the viewable area of the page (excludes scrollbars).



getRegion
Region | Array getRegion ( el )
Returns the region position of the given element. The element must be part of the DOM tree to have a region (display:none or elements not appended return false).

Parameters:
el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements.

Returns: Region | Array
A Region or array of Region instances containing "top, left, bottom, right" member data.


getDocumentWidth
Int getDocumentWidth ( )
Returns the width of the document.

Returns: Int
The width of the actual document (which includes the body and its margin).


getDocumentScrollTop
Int getDocumentScrollTop ( document )
Returns the top scroll value of the document

Parameters:
document (optional) The document to get the scroll value of

Returns: Int
The amount that the document is scrolled to the top


getXY
Array getXY ( el )
Gets the current position of an element based on page coordinates. Element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).

Parameters:
el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements

Returns: Array
The XY position of the element(s)

gmail fragment identifiers

http://mail.google.com/mail/?shva=1#inbox/1169c4949d7598d6

#後面那一串就是 fragment identifiers, 可以讓 JavaScript keep 目前的 state, 像是目前使用者正在看的 tab, 不會因為使用者 refresh 之後就回到最初的狀態, 比較特別的是#後面還有inbox/, 看來可以好好利用一下。

Menu Editor :: Firefox Add-ons

Menu Editor :: Firefox Add-ons - 可有效去除context menu過長的毛病,尤其在過多的add-on都想佔據contenxt menu的情況下。

Rearrange or remove menuitems from the main context menu (right-click menu) and main menubar (File Edit View etc.)

IE fix

作者 coolfiretw (冷炎TM) 看板 Fuck
標題 Re: IE好像爛掉了
時間 Thu Nov 29 00:13:43 2007

※ 引述《whchu (風吹隨便倒...)》之銘言:
: IE用到一半,沒事就會出現ntdll.dll錯誤,然後就死掉。
: 拿winxp光碟片用修復裝一次也修不好。
: 幹!
試試看萬能解決法吧.....

重新註冊全部的DLL:
for %1 in (%windir%\system32\*.dll) do regsvr32.exe /s %1

反正一堆鳥問題,大部份都能解決.

12/02/2007

javascript pseudo-protocol HREF is harmful

http://kitty.2y.idv.tw/~chfang/test-revised.htm
odd problem about innerHTML & img under IE - comp.lang.javascript | Google 網上論壇

Executing a javascript pseudo-protocol HREF in IE browsers (at least up
to 6) convinces the browser that it about to navigate away from the
current page (as would be the normal consequence of following the HREF
of a link) and puts the browser into a 'waiting' stat in witch various
facilities stop working (or working in the previously expected way). One
of the more often observed symptoms is the browser failing to
load/display new images. This is usually reported as the unexpected
stopping of image swapping with rollovers, but any failure in an image
related operation after the execution of a javascript pseudo-protocol
HREF might be attributed to this.

Richard.

crisp's blog » Blog Archive » The useless javascript: pseudo-protocol

It comes down to this: when you use this in for instance an href-attribute you are most probably misusing an anchor-element for something that is not related to navigation. "You should not use a link for anything else but a link" - it's the basis of the web: links are for navigation and not for in-page behavior*. Got that?

Note the 'return false': this prevents a JS-capable client to actually start navigating to 'alternative.html'. That brings us to another problem with the javascript: pseudo-protocol, namely the fact that IE (at least up to version 6) enters a different 'state' when the href of a link is being followed no matter if it contains an actual link or a javascript function. This is most noticable when you have animated gifs on your page: when such a link is clicked they stop animating, and there are probably more problems that can arise from this 'state'-change in Internet Explorer. Wrapping your function-call in the void() operator doesn't change that either.

Re: A tag: href vs. onClick
update: 可以用< a id="next" href="#">
YUE.on('next', 'click', function(e){
doSomething();
YUE.stopEvent(e);
return fasle
})