日期:2014-05-16  浏览次数:20378 次

js跨域实现统计
前端js部分
function addJs(file)
{
	var scriptId=arguments[1];
	var funExec=arguments[2];
	var funcLoading=arguments[3];
	var expire=arguments[4];
	var expFunc=arguments[5];
	var parent=document.getElementsByTagName('head')[0];
	if($('scriptId'))
	{
		var script=$('scriptId');
		script.src=file;
	}
	else
	{
		var script=document.createElement('SCRIPT');
		script.setAttribute('type', 'text/javascript');
		script.setAttribute('src', file);
		script.setAttribute('id', scriptId);
		parent.appendChild(script);
	}
	script.onreadystatechange =  function (){
	if(typeof(funExec)=='function'){if(script.readyState == 'complete' || script.readyState =="loaded"){funExec()}}
	if(script.readyState == 'loading'&&typeof(funcLoading)=='function'){t=setTimeout(funcLoading ,expire )}
	};
	script.onload = function (){(typeof(funExec)=='function')?funExec():""};
	return script;
}

function downcount(){
	addJs("http://localhost/count.php?click=1&"+Math.random());
	//jQuery.getJSON(
	//  "http://www.ttcj.cn/count.php?click=1&"+Math.random(),
	//  function(json){
	//	$("count").innerHTML=json;
	//  }
	//);
}

function $(id){
	return document.getElementById(id);
}


count.php处理部分
if($_GET['click']==1){
	if(file_exists("count.txt")){
		$count=file_get_contents('count.txt');
		$count=$count+1;
		Tools::writeFile($count,'count.txt');
		echo 'document.getElementById("count").innerHTML='.$count;
	}else{
		Tools::writeFile('1000','count.txt');
		echo 'document.getElementById("count").innerHTML=12';
		//echo '12';
	}
}else{
	$count=file_get_contents('count.txt');
	echo 'document.write("'.$count.'")';
}