下面是一个js跨域请求的小实现
View Code
window._naq = {};//外部调用方法 _naq.push = function(obj){//obj = {"k":"v","k":"v"} var url = nmurl+"?"; jsonobj = eval(obj); for(var k in jsonobj){ url += k+"="+jsonobj[k]+"&"; } //url+= "accessLog="+($.cookie("access_log") || 0) +"&userId="+($.cookie("userId") || 0); ajax(encodeURI(url)); } function ajax(msg){//请求msg:http://www.xxx.com/a.gif?a=1&b=2... var img = new Image(); if(msg.indexOf("?")>1){ if (msg.lastIndexOf("&") + 1 == msg.length) { img.src = msg + "datetime=" + new Date().getTime(); }else{ img.src = msg + "&datetime=" + new Date().getTime(); } }else{ img.src = msg+"?datetime="+new Date().getTime(); } } //_naq.push({"url":encodeURIComponent(window.location.href),"accessLog":$.cookie("access_log"),"userId":$.cookie("userId")});//默认一次pv _naq.push({"pv":"true"});//默认一次pv
html页面按照如下方式进行调用
代码片段...<a href="#" onclick="_naq.push({"pv":"true"})">记录一次点击</a>