Search

1/30/2007

bookmarklet: getTitle()

一個簡單的 bookmarklet,可以快速的複製某個網頁的 title,這樣如果要寫 blog 的話比較方便,設定position是因為怕版型亂調,設定zIndex是怕被網頁的內容蓋住。點一下可以取消。
要注意的是 if(input.parentNode){input.parentNode.removeChild(input);},這邊如果寫 if(input) 會 always true,even被從DOM tree remove 掉,所以這邊檢查的是 input.parentNode
genTitle


<a href="javascript:
var title = document.getElementsByTagName('title')[0];
titleText = (typeof title == 'undefined')?
'Error: no title element': title.innerHTML;
if(typeof input=='undefined')
input = document.createElement('input');

var _scrolltop;
if (document.documentElement && document.documentElement.scrollTop){
_scrolltop = document.documentElement.scrollTop;
}
else if(document.body){
_scrolltop = document.body.scrollTop;
}
input.style.fontFamily = 'arial';
input.style.border= '1px solid gray';
input.style.padding = '1px 5px';
input.style.fontSize = '1em';
input.style.width = '20em';
input.style.backgroundColor = '#eee';
input.style.position = 'absolute';
input.style.left = '0';
input.style.top = _scrolltop+'px';
input.style.zIndex = '999';
input.value = titleText;
if(document.body)
document.body.insertBefore(input, document.body.firstChild);

input.select();
document.onclick = function(e){
if(input.parentNode){
input.parentNode.removeChild(input);
}
document.onclick = null;
};
void 0; ">genTitleNew</a>

沒有留言: