当前页面: 开发资料首页 → Javascript 专题 → JavaScript应用实例:点出统计器
JavaScript应用实例:点出统计器
摘要: JavaScript应用实例:点出统计器
<tr>
<td>
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('
');
document.body.appendChild(this.oCO);
document.getElementById("ClickOut").innerHTML = '';
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;
演示:
运行代码框
<textarea class=fm id=code style="WIDTH: 516px; HEIGHT: 172px" rows=12 cols=82> HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<head>
Layout 16
</head>
<body>
1) Content here. 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.
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
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
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
2) Navigation here. 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
<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('
');
document.body.appendChild(this.oCO);
document.getElementById("ClickOut").innerHTML = '';
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 event
document.onclick = click_tmp;
</script>
</body>
</textarea>
<input style="CURSOR: hand" onfocus=this.blur() onclick="runEx('code')" type=button value=运行代码> <input style="WIDTH: 95px; CURSOR: hand" onfocus=this.blur() onclick=cycode(code) type=button value=复制到剪贴板 name=Submit> <input style="CURSOR: hand" onfocus=this.blur() onclick=svcode(code) type=button value=另存代码> [Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]
</td>
</tr>
</table>
↑返回目录
前一篇:
奇招 Javascript暴虐查找法
后一篇:
符合web标准的媒体播放器代码