js常用操作代码

时间:2023-03-09 04:54:35
js常用操作代码

页面前进后退
<input type=button value=刷新 onclick="window.location.reload()">
<input type=button value=前进 onclick="window.history.go(1)">
<input type=button value=后退 onclick="window.history.go(-1)">
<input type=button value=前进 onclick="window.history.forward()">
<input type=button value=后退 onclick="window.history.back()"> 后退+刷新<input type=button value=后退 onclick="window.history.go(-1);window.location.reload()">

"window.location.href"、"location.href"是本页面跳转

"parent.location.href"是上一层页面跳转

"top.location.href"是最外层的页面跳转

ajax 同步操作

async : false, // (默认: true) 默认设置下,所有请求均为异步请求。如果需要发送同步请求,请将此选项设置为
        // false。注意,同步请求将锁住浏览器,用户其它操作必须等待请求完成才可以执行。

string - json
JSONObject jsonObject = new JSONObject(jsonString);

//js时间格式类
Date.prototype.Format = function(fmt)   
{ //author: meizz    
  var o = {   
    "M+" : this.getMonth()+1,                 //月份    
    "d+" : this.getDate(),                    //日    
    "h+" : this.getHours(),                   //小时    
    "m+" : this.getMinutes(),                 //分    
    "s+" : this.getSeconds(),                 //秒    
    "q+" : Math.floor((this.getMonth()+3)/3), //季度    
    "S"  : this.getMilliseconds()             //毫秒    
  };   
  if(/(y+)/.test(fmt))   
    fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));   
  for(var k in o)   
    if(new RegExp("("+ k +")").test(fmt))   
  fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));   
 return fmt;   
};

//遍历

$(data).each(function(index ,elm){
        alert(data[index]);
    });

<a href="http://www.baidu.com" target="_blank">新窗口打开</a>

//屏幕回滚
window.scrollTo(0, 0);

//javascript 转化为bool对象的规则
数据类型     转换为bool后的值
null     FALSE
undefined     FALSE
Object     TRUE
function     TRUE
0     FALSE
1     TRUE
0、1之外的数字     TRUE
字符串     TRUE
""(空字符串)     FALSE
如果&&左侧表达式的值为真值,则返回右侧表达式的值;否则返回左侧表达式的值。
如果||左侧表达式的值为假值,则返回右侧表达式的值;否则返回左侧表达式的值。

js获取来源页地址方法: var url=document.referrer;

java 获取来源页面地址:String url = request.getHeader("Referer");

Window 尺寸

有三种方法能够确定浏览器窗口的尺寸(浏览器的视口,不包括工具栏和滚动条)。

对于Internet Explorer、Chrome、Firefox、Opera 以及 Safari:

window.innerHeight - 浏览器窗口的内部高度
    window.innerWidth - 浏览器窗口的内部宽度

对于 Internet Explorer 8、7、6、5:

document.documentElement.clientHeight
    document.documentElement.clientWidth

或者

document.body.clientHeight
    document.body.clientWidth

实用的 JavaScript 方案(涵盖所有浏览器):

isNaN() 非数字返回 true