JavaScript中常用的方法——(不定时更新)

解析从其他页面跳转过来时传递的参数:

function parseUrl() {
	var url = location.href;
	var i = url.indexOf('?');
	if (i == -1) return;
	var querystr = url.substr(i + 1);
	var arr1 = querystr.split('&');
	var arr2 = new Object();
	for (i in arr1) {
		var ta = arr1[i].split('=');
		arr2[ta[0]] = ta[1];
	}
	return arr2;
}

从location.href中获取参数,返回一个数组。使用方法:假如我们传递的参数为:a.html?userId=1,那么我们可以这样获取:parseUrl()["userId

"] 。这样我们就能拿到值为 1 的 userID 。

2017年3月12日15:28:43---update:上面的parseUrl()的返回值要先判断一下,不然如果location.href中没有带参数的话,拿值会报错!!
使用:
var arr = parseUrl();
if (arr != null && arr) {
        arr['id'];
}


最近遇到一个需求:前面页面将后台返回的Json数据转成字符串拼接成url给我,数据中有我所有页面请求时的参数和接口名,我要通过每个页面的关键字拿到参数和接口名!我将方法封装到index.html中,其他页面都是从index.html的div中load进去的。封装的代码如下:
function getSelfRecordUrlAndParametersByTableName(tableName) {
        var obj = null;
        if (window.localStorage.getItem(tableName + "")) {
              //缓存中有该数据,从缓存中解析成JsonObject返回
              var cacheData = JSON.parse(window.localStorage.getItem(tableName + ""));
              obj = new Object();
              obj.interface = cacheData.interface;
              obj.parameters = cacheData.parameters;
              return obj;
        }

        //缓存中没有,从上一个页面传递过来的参数中解析
        var arr = parseUrl();
        if (arr != null && arr) {
              var datas = arr['datas'];
              var datasJsonObj = JSON.parse(datas);
              if (datasJsonObj.successInfo.success == true) {
                   $.each(datasJsonObj.returnListInfo, function(index, data) {
                       if (tableName == data.tableName) {
                            //首先将该页面数据缓存下来
                            window.localStorage.setItem(tableName + "", JSON.stringify(data));

                            obj = new Object();
                            obj.interface = data.interface;
                            obj.parameters = data.parameters;
                        }
                    });
              }
        }

        return obj;
}

这里的parseUrl是上面的方法!由于学前端不久,代码可能有不严谨和性能上的问题,但是能解决问题的代码才是王道!!


判断一个字符串是否是空字符串、null、undefined类型:

function isNull(str) {
	if (!str || str.length == 0 || str == " ") {
		return true;
	} else {
		return false;
	}
}


去除字符串前后的空格:

function delTrim(str) {
	return str.replace(/(^\s*)|(\s*$)/g, "");
}


iframe自适应子页面高度:

var browserVersion = window.navigator.userAgent.toUpperCase();
var isOpera = browserVersion.indexOf("OPERA") > -1 ? true : false;
var isFireFox = browserVersion.indexOf("FIREFOX") > -1 ? true : false;
var isChrome = browserVersion.indexOf("CHROME") > -1 ? true : false;
var isSafari = browserVersion.indexOf("SAFARI") > -1 ? true : false;
var isIE = (!!window.ActiveXObject || "ActiveXObject" in window);
var isIE9More = (!-[1, ] == false);

function reinitIframe(iframeId, minHeight) {
      try {
           var iframe = document.getElementById(iframeId);
           var bHeight = 0;
           if (isChrome == false && isSafari == false) {
                bHeight = iframe.contentWindow.document.body.scrollHeight;
           }

           var dHeight = 0;
           if (isFireFox == true) {
                dHeight = iframe.contentWindow.document.documentElement.offsetHeight + 2;
           } else if (isIE == false && isOpera == false) {
                dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
           } else if (isIE == true && isIE9More) { //ie9+
                var heightDeviation = bHeight - eval("window.IE9MoreRealHeight" + iframeId);
           	if (heightDeviation == 0) {
                	bHeight += 3;
           	} else if (heightDeviation != 3) {
                	eval("window.IE9MoreRealHeight" + iframeId + "=" + bHeight);
                	bHeight += 3;
           	}
      	   } else { //ie[6-8]、OPERA
           	bHeight += 3;
      	   }

      	   var height = Math.max(bHeight, dHeight);
      	   if (height < minHeight) {
           	height = minHeight;
      	   }
           iframe.style.height = height + "px";
      } catch (ex) {}
}

function startInit(iframeId, minHeight) {
   eval("window.IE9MoreRealHeight" + iframeId + "=0");
   window.setInterval("reinitIframe('" + iframeId + "'," + minHeight + ")", 100);
}
// 调用
startInit("iframe", 100);

这个方法是上网找了很多方法,就这个管用。。。

下面贴一下原文链接:

点击打开链接


自定义Toast弹出消息框:

function Toast(msg, duration) {
    duration = isNaN(duration) ? 3000 : duration;
    var m = document.createElement('div');
    m.innerHTML = msg;
    m.style.cssText = "width: 20%;min-width: 150px;opacity: 0.7;height: 30px;color: rgb(255, 255, 255);line-height: 30px;text-align: center;border-radius: 5px;position: fixed;bottom: 50%;left: 40%;z-index: 999999;background: rgb(0, 0, 0);font-size: 12px;";
    document.body.appendChild(m);
    setTimeout(function() {
        var d = 0.5;
        m.style.webkitTransition = '-webkit-transform ' + d + 's ease-in, opacity ' + d + 's ease-in';
        m.style.opacity = '0';
        setTimeout(function() { document.body.removeChild(m) }, d * 1000);
    }, duration);
}

效果为:





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值