<script language="JavaScript"> <!-- var caution = false function setCookie(name, value, expires, path, domain, secure) { var curCookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : ""); if (!caution || (name + "=" + escape(value)).length <= 4000) document.cookie = curCookie else if (confirm("Cookie exceeds 4KB and will be cut!")) document.cookie = curCookie } function getCookie(name) { var prefix = name + "=" var cookieStartIndex = document.cookie.indexOf(prefix) if (cookieStartIndex == -1) return null var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length) if (cookieEndIndex == -1) cookieEndIndex = document.cookie.length return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex)) } function deleteCookie(name, path, domain) { if (getCookie(name)) { document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT"; } } function fixDate(date) { var base = new Date(0) var skew = base.getTime() if (skew > 0) date.setTime(date.getTime() - skew) } var now = new Date() fixDate(now) now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000) var visits = getCookie("counter") if (!visits) visits = 1 else visits = parseInt(visits) + 1 //setCookie("counter", visits, now) document.write("您是第" + visits + "访客!") // --> </script>
每次页面访问,页面就会加1,获得ip后还可进一步分析PV,UV数据。
<script type="text/javascript"> document.write(document.referrer);//上级url document.write(document.URL);//当前url document.write(document.domain);//域名 document.write(document.cookie);//cookie值 document.write(document.lastModified);//当前文档修改最新时间 document.write(document.title);//当前页面标题 </script>
上面js可以获得用户从那个页面跳转到你的页面,实现浏览轨迹 上下游页面的url。
<!--页面停留了多少时间--> <form name=forms> <p>你在网页已停留了:</p> <input type=text name=input size="12"> <script language="javascript"> var second=0; var minute=0; var hour=0; idt=window.setTimeout("interval();",1000); function interval(){ second++; if(second==60) second=0;minute+=1; if(minute==60) minute=0;hour+=1; document.forms.input.value=hour+"时"+minute+"分"+second+"秒"; idt=window.setTimeout("interval();",1000); } </script> </form>
判断从哪个搜索工具找到你的网站,只要根据:
<script language="javascript"> document.write(document.referrer);//上级url </script>
就可分辨。
根据用户的ip信息来获取到地域熟悉,我们可以使用第三方的ip接口来实现获取地理位置,如新浪接口:
<script language="javascript" type="text/javascript" src="http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js"></script> <script type="text/javascript"> document.write("欢迎来自:"+remote_ip_info.country+remote_ip_info.province+remote_ip_info.city+"的朋友!"); </script>
可以使用jquery的$.getJSON
,以jquery手册为例:
$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?", function(data){ $.each(data.items, function(i,item){ $("<img/>").attr("src", item.media.m).appendTo("#images"); if ( i == 3 ) return false; }); });
跨域说明:只支持get方式传值,url里面callback一定要设置,返回数据为json数据结构。