JS实用小功能

获取用户IP、UA、城市
//userAgent 属性是一个只读的字符串,声明了浏览器用于 HTTP 请求的用户代理头的值。一般来讲,它是在 navigator.appCodeName 的值之后加上斜线和 navigator.appVersion 的值构成的。

var UA = navigator.userAgent;

var d = navigator.appName;  //返回Netscape或浏览器全名,兼容性考虑
var c = navigator.appVersion;// 返回浏览器的平台和版本信息 
var b = navigator.appCodeName;// 声明了浏览器的代码名
<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script> 
<script type="text/javascript">
var ip  = returnCitySN["cip"];
var city = returnCitySN["cname"];
var cid = returnCitySN["cid"];
function a(){
document.getElementById("ip").innerHTML = ip;
document.getElementById("city").innerHTML = city;
document.getElementById("cid").innerHTML = cid;
}
</script>
  </head>
  
  <body οnlοad="a()">
    <table>
    <ul id="boss">
    <li id="ip"></li>
    <li id="city"></li>
    <li id="cid"></li>
    <li id="souhu">搜狐<a href="http://pv.sohu.com/cityjson">http://pv.sohu.com/cityjson</a></li>
    <li id="xinlang">新浪<a href="http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js">http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js</a></li>
</ul>
    </table>
  </body>


操作cookie
function getCookie(c_name) {
if (document.cookie.length > 0) {
c_start = document.cookie.indexOf(c_name + "=");
if (c_start != -1) {
c_start = c_start + c_name.length + 1;
c_end = document.cookie.indexOf(";", c_start);
if (c_end == -1) {
c_end = document.cookie.length;
}
return unescape(document.cookie.substring(c_start, c_end));
}
}
return "";
}
function setCookie(c_name, value, expiredays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + expiredays);//按天数 
//exdate.setTime(exdate.getTime() + 60*1000);//按毫秒
document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString())+";path=/";
}
function checkCookie(name) {
var username = getCookie(name);
if (username != null && username != "") {
alert(name+"存储的cookie为:"+username);
} else {
alert("没有key为"+name+"的cookie信息");
}
}


JS手机振动
var vibrateInterval;// 开始震动
function startVibrate(duration) {
if ("vibrate" in navigator) { 
  navigator.vibrate(duration);
}
}// 停止震动
function stopVibrate() {
// 清除间隔和停止持续振动
if(vibrateInterval) clearInterval(vibrateInterval);
navigator.vibrate(0);
}//在给定的持续时间和间隔时开始持续的振动
//假定一个数字值
function startPeristentVibrate(duration, interval) {
vibrateInterval = setInterval(function() {
startVibrate(duration);
}, interval);
}
startVibrate(1000);


JS禁止浏览器后退
$(document).ready(function(e) {
var counter = 0;
if (window.history && window.history.pushState) {
$(window).on('popstate', function () {
window.history.pushState('forward', null, '');
window.history.forward(1);
});
}
window.history.pushState('forward', null, ''); //在IE中必须得有这两行
window.history.forward(1);
});
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值