关于Javascript的一些实践

  • 跨浏览器获取窗口左边和上边的位置
var leftPos = (typeof window.screenLeft == 'number') ? window.screenLeft : window.screenX;
var topPos = (type window.screenTop == 'number') ? window.screenTop : window.screenY;
  • window.open弹出窗口被阻止
var blocked = false;
try{
    var tip = window.open("http://www.baidu.com", "_blank");
    if(tip == null){
        blocked = true;
    }
} catch (ex) {
    blocked = true;
}
if(blocked){
    alert('窗口被阻止了');
}
  • 手机类型判断
var BrowserInfo = {

userAgent: navigator.userAgent.toLowerCase()

isAndroid: Boolean(navigator.userAgent.match(/android/ig)),

isIphone: Boolean(navigator.userAgent.match(/iphone|ipod/ig)),

isIpad: Boolean(navigator.userAgent.match(/ipad/ig)),

isWeixin: Boolean(navigator.userAgent.match(/MicroMessenger/ig)),

}
  • 返回字符串长度,汉子计数为2
function strLength(str) {

var a = 0;

for (var i = 0; i < str.length; i++) {

if (str.charCodeAt(i) > 255)

a += 2;//按照预期计数增加2

else

a++;

}

return a;

}
  • 获取url中的参数
function GetQueryStringRegExp(name,url) {

var reg = new RegExp("(^|\?|&)" + name + "=([^&]*)(\s|&|$)", "i");

if (reg.test(url)) return decodeURIComponent(RegExp.$2.replace(/+/g, " ")); return "";

}
  • js弹出新窗口全屏
var tmp = window.open("about:blank", "", "fullscreen=1")

tmp.moveTo(0, 0);

tmp.resizeTo(screen.width + 20, screen.height);

tmp.focus();

tmp.location.href = '//www.che168.com/pinggu/eva_' + msgResult.message[0] + '.html';

var config_ = "left=0,top=0,width=" + (window.screen.Width) + ",height=" + (window.screen.Height);

window.open('//www.che168.com/pinggu/eva_' + msgResult.message[0] + '.html', "winHanle", config_);

//模拟form提交打开新页面

var f = document.createElement("form");

f.setAttribute('action', '//www.che168.com/pinggu/eva_' + msgResult.message[0] + '.html');

f.target = '_blank';

document.body.appendChild(f);

f.submit();
  • js 判断浏览器
判断是否是 IE 浏览器

if (document.all){

alert(”IE浏览器”);

}else{

alert(”非IE浏览器”);

}

if (!!window.ActiveXObject){

alert(”IE浏览器”);

}else{

alert(”非IE浏览器”);

}

判断是IE几

var isIE=!!window.ActiveXObject;

var isIE6=isIE&&!window.XMLHttpRequest;

var isIE8=isIE&&!!document.documentMode;

var isIE7=isIE&&!isIE6&&!isIE8;

if (isIE){

if (isIE6){

alert(”ie6″);

}else if (isIE8){

alert(”ie8″);

}else if (isIE7){

alert(”ie7″);

}

}
  • 判断浏览器
function getOs() {

if (navigator.userAgent.indexOf("MSIE 8.0") > 0) {

return "MSIE8";

}

else if (navigator.userAgent.indexOf("MSIE 6.0") > 0) {

return "MSIE6";

}

else if (navigator.userAgent.indexOf("MSIE 7.0") > 0) {

return "MSIE7";

}

else if (isFirefox = navigator.userAgent.indexOf("Firefox") > 0) {

return "Firefox";

}

if (navigator.userAgent.indexOf("Chrome") > 0) {

return "Chrome";

}

else {

return "Other";

}

}
  • JS判断两个日期大小 适合 2012-09-09 与2012-9-9 两种格式的对比
//得到日期值并转化成日期格式,replace(/-/g, "/")是根据验证表达式把日期转化成长日期格式,这样再进行判断就好判断了

function ValidateDate() {

var beginDate = $("#t_datestart").val();

var endDate = $("#t_dateend").val();

if (beginDate.length > 0 && endDate.length>0) {

var sDate = new Date(beginDate.replace(/-/g, "/"));

var eDate= new Date(endDate.replace(/-/g, "/"));

if (sDate > eDate) {

alert('开始日期要小于结束日期');

return false;

}

}

}
  • 回车提交
$("id").onkeypress = function (event) {

event = (event) ? event : ((window.event) ? window.event : "")

keyCode = event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode);

if (keyCode == 13) {

$("SubmitLogin").onclick();

}

}
  • ajax请求
C.ajax = function (args) {

var self = this;

this.options = {

type: 'GET',

async: true,

contentType: 'application/x-www-form-urlencoded',

url: 'about:blank',

data: null,

success: {},

error: {}

};

this.getXmlHttp = function () {

var xmlHttp;

try {

xmlhttp = new XMLHttpRequest();

}

catch (e) {

try {

xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");

}

catch (e) {

xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");

}

}

if (!xmlhttp) {

alert('您的浏览器不支持AJAX');

return false;

}

return xmlhttp;

};

this.send = function () {

C.each(self.options, function (key, val) {

self.options[key] = (args[key] == null) ? val : args[key];

});

var xmlHttp = new self.getXmlHttp();

if (self.options.type.toUpperCase() == 'GET') {

xmlHttp.open(self.options.type, self.options.url + (self.options.data == null ? "" : ((/[?]$/.test(self.options.url) ? '&' : '?') + self.options.data)), self.options.async);

}

else {

xmlHttp.open(self.options.type, self.options.url, self.options.async);

xmlHttp.setRequestHeader('Content-Length', self.options.data.length);

}

xmlHttp.setRequestHeader('Content-Type', self.options.contentType);

xmlHttp.onreadystatechange = function () {

if (xmlHttp.readyState == 4) {

if (xmlHttp.status == 200 || xmlHttp.status == 0) {

if (typeof self.options.success == 'function') self.options.success(xmlHttp.responseText);

xmlHttp = null;

}

else {

if (typeof self.options.error == 'function') self.options.error('Server Status: ' + xmlHttp.status);

}

}

};

xmlHttp.send(self.options.type.toUpperCase() == 'POST' ? self.options.data.toString() : null);

};

this.send();

};
  • 整个UL 点击事件 加在UL里的onclick里
function CreateFrom(url, params) {

var f = document.createElement("form");

f.setAttribute("action", url);

for (var i = 0; i < params.length; i++) {

var input = document.createElement("input");

input.setAttribute("type", "hidden");

input.setAttribute("name", params[i].paramName);

input.setAttribute("value", params[i].paramValue);

f.appendChild(input);

}

f.target = "_blank";

document.body.appendChild(f);

f.submit();

};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值