JavaScript應用執行個體:點出統計器

來源:互聯網
上載者:User
javascript|統計|應用執行個體

ff   預設不讓改 status
opera9   測試通過
ie6   測試通過

這東西是給統計部門用的,分析使用者習慣以改良網站布局。
只是寫著玩的小東西,所以很多地方不太理想。

save() 方法是儲存記錄的,沒仔細做,應該再加個判斷瀏覽器,然後決定用 img 還是 iframe,以保證 request 一定能發出去.
onclick() 方法是觸發事件時執行的

使用方法:

在任意頁面中加入

CODE:
script src="clickout.js"></script>

最好是放在 </body> 前面,目的是防止 onclick 事件覆蓋。

clickout.js

CODE:

/*
* 說明:點出統計器
* 作者:鄧威
* 日期:2006-07-25
* 版本:v1.0
*/

function ClickOut()
{
    this.oCO        = null;
    this.src_onclick    = null;
    this.isDEBUG        = true || "status"; // 改成 this.isDEBUG = "status"; 就可以在 window.status 裡顯示了

    this.debug        = function(arg) {
        if( this.isDEBUG == "status" ) window.status = arg.toString();
        else if ( this.isDEBUG ) alert(arg.toString());
    }

    this.save        = function() {       
        var sUrl = "clickout.php?";
        sUrl += "tag=" + encodeURIComponent(this.click_tag);
        sUrl += "&type=" + encodeURIComponent(this.click_type);
        sUrl += "&src=" + encodeURIComponent(this.click_src);
        sUrl += "&text=" + encodeURIComponent(this.click_text);
        this.oCO = document.createElement('<div id="ClickOut" style="width:0px;height:0px;"></div>');
        document.body.appendChild(this.oCO);   
        document.getElementById("ClickOut").innerHTML =  '<img src="' + sUrl + '" style="display: none;" width="0px" height="0px" \/>';
        return true;
    }

    this.onclick        = function(args) {       
        if( this.src_onclick != null ) this.src_onclick(e);
        var obj = event.srcElement;
        var tag = obj.tagName.toLowerCase();
       
        this.click_id        = obj.id?obj.id:null;
        this.click_class    = obj.className?obj.className:null;
        this.click_name        = obj.name?obj.name:null;
        this.click_tag        = tag;
        this.click_parent    = obj;
        this.click_obj        = obj;
        this.click_path        = tag;

        if( tag == "a" ) {
            this.click_type = "text";
            this.click_src = obj.href;
            this.click_text = obj.innerHTML;
        }
        else if ( tag == "img" ) {
            this.click_type = "pic";
            this.click_src = obj.src;
            this.click_text = obj.alt;
        }
        else if ( tag == "font" || tag == "b" || tag == "strong" ) {
            this.click_type = "text";
            this.click_src = "";
            this.click_text = obj.innerHTML;
        }
        else {
            this.click_type = "layer";
            this.click_src = "" ;
            this.click_text = obj.innerHTML;
        }
       
        this.GetParent();

        this.debug(this.click_path);
        doSave();
    }

    this.GetParent        = function() {       
        if( !this.click_parent.parentNode.tagName ) return;
        this.click_parent = this.click_parent.parentNode;       
        this.click_path = this.click_parent.tagName.toLowerCase() + "[ " +
            (this.click_parent.id?("ID:" + this.click_parent.id):"") +
            (this.click_parent.className?(" ,CLASS:" + this.click_parent.className):"") +
            (this.click_parent.name?(" ,NAME:" + this.click_parent.name):"") +
            " ]" + ">" + this.click_path;   
        this.GetParent();
    }
}

// 執行個體
var objCO = new ClickOut();
objCO.src_onclick = document.onclick;

function click_tmp() {
    objCO.onclick();
}
function doSave() {
    objCO.save();
}

// bind event
document.onclick = click_tmp;

示範:

運行代碼框

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head><title>Layout 16</title><meta http-equiv="content-type" content="text/html; charset=iso-8859-1"><meta name="generator" content="HAPedit 3.1"><style type="text/css">html,body{margin:0;padding:0}body{font: 76% arial,sans-serif}p{margin:0 10px 10px}a{display:block;color: #981793;padding:10px}div#header h1{height:80px;line-height:80px;margin:0; padding-left:10px;background: #EEE;color: #79B30B}div#content p{line-height:1.4}div#navigation{background:#B9CAFF}div#extra{background:#FF8539}div#footer{background: #333;color: #FFF}div#footer p{margin:0;padding:5px 10px}div#wrapper{float:left;width:100%}div#content{margin-right: 400px}div#navigation{float:left;width:200px;margin-left:-200px}div#extra{float:left;width:200px;margin-left:-400px}div#footer{clear:left;width:100%}</style></head><body><div id="container"><div id="header"><h1>Header</h1></div><div id="wrapper"><div id="content"><p><strong>1) Content here.</strong> column long long column very long fill fill fill long text text column text silly very make long very fill silly make make long make text fill very long text column silly silly very column long very column filler fill long make filler long silly very long silly silly silly long filler make column filler make silly long long fill very.</p><p>very make make fill silly long long filler column long make silly silly column filler fill fill very filler text fill filler column make fill make text very make make very fill fill long make very filler column very long very filler silly very make filler silly make make column column </p><p>fill long make long text very make long fill column make text very silly column filler silly text fill text filler filler filler make make make make text filler fill column filler make silly make text text fill make very filler column very </p><p>column text long column make silly long text filler silly very very very long filler fill very fill silly very make make filler text filler text make silly text text long fill fill make text fill long text very silly long long filler filler fill silly long make column make silly long column long make very </p></div></div><div id="navigation"><p><strong>2) Navigation here.</strong> long long fill filler very fill column column silly filler very filler fill fill filler text fill very silly fill text filler silly silly filler fill very make fill column text column very very column fill fill very silly column silly silly fill fill long filler </p></div><div id="extra"><p><strong>3) More stuff here.</strong> very text make long column make filler fill make column column silly filler text silly column fill silly fill column text filler make text silly filler make filler very silly make text very very text make long filler very make column make silly column fill silly column long make silly filler column filler silly long long column fill silly column very </p></div><div id="footer"><p>Here it goes the footer</p></div></div><script>/* * 說明:點出統計器 * 作者:鄧威 * 日期:2006-07-25 * 版本:v1.0 */function ClickOut(){this.oCO= null;this.src_onclick= null;this.isDEBUG= true || "status";this.debug= function(arg) {if( this.isDEBUG == "status" ) window.status = arg.toString();else if ( this.isDEBUG ) alert(arg.toString());}this.save= function() {var sUrl = "clickout.php?";sUrl += "tag=" + encodeURIComponent(this.click_tag);sUrl += "&type=" + encodeURIComponent(this.click_type);sUrl += "&src=" + encodeURIComponent(this.click_src);sUrl += "&text=" + encodeURIComponent(this.click_text);this.oCO = document.createElement('<div id="ClickOut" style="width:0px;height:0px;"></div>');document.body.appendChild(this.oCO);document.getElementById("ClickOut").innerHTML = '<img src="' + sUrl + '" style="display: none;" width="0px" height="0px" \/>';return true;}this.onclick= function(args) {if( this.src_onclick != null ) this.src_onclick(e);var obj = event.srcElement;var tag = obj.tagName.toLowerCase();this.click_id= obj.id?obj.id:null;this.click_class= obj.className?obj.className:null;this.click_name= obj.name?obj.name:null;this.click_tag= tag;this.click_parent= obj;this.click_obj= obj;this.click_path= tag;if( tag == "a" ) {this.click_type = "text";this.click_src = obj.href;this.click_text = obj.innerHTML;}else if ( tag == "img" ) {this.click_type = "pic";this.click_src = obj.src;this.click_text = obj.alt;}else if ( tag == "font" || tag == "b" || tag == "strong" ) {this.click_type = "text";this.click_src = "";this.click_text = obj.innerHTML;}else {this.click_type = "layer";this.click_src = "" ;this.click_text = obj.innerHTML;}this.GetParent();this.debug(this.click_path);doSave();}this.GetParent= function() {if( !this.click_parent.parentNode.tagName ) return;this.click_parent = this.click_parent.parentNode;this.click_path = this.click_parent.tagName.toLowerCase() + "[ " + (this.click_parent.id?("ID:" + this.click_parent.id):"") + (this.click_parent.className?(" ,CLASS:" + this.click_parent.className):"") + (this.click_parent.name?(" ,NAME:" + this.click_parent.name):"") + " ]" + ">" + this.click_path;this.GetParent();}}// 執行個體var objCO = new ClickOut();// 儲存原有的 onclick 事件objCO.src_onclick = document.onclick;function click_tmp() {objCO.onclick();}function doSave() {objCO.save();}// bind eventdocument.onclick = click_tmp;</script></body></html>

    [Ctrl+A 全部選擇 提示:你可先修改部分代碼,再按運行]



相關文章

Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。