记录 《JavaScript高级程序设计》中几个函数

1.查询字符串参数

function getQueryStringArgs(){ 
  //取得查询字符串并去掉开头的问号
  var qs = (location.search.length > 0 ? location.search.substring(1) : ""), 
  
  //保存数据的对象
  args = {}, 
  
  //取得每一项
  items = qs.length ? qs.split("&") : [], 
  item = null, 
  name = null, 
  value = null, 
  //在 for 循环中使用
  i = 0, 
  len = items.length; 
  //逐个将每一项添加到 args 对象中
  for (i=0; i < len; i++){ 
    item = items[i].split("="); 
    name = decodeURIComponent(item[0]); 
    value = decodeURIComponent(item[1]); 
    if (name.length) { 
      args[name] = value; 
    } 
  } 
  return args; 
}

//假设查询字符串是?q=javascript&num=10 
var args = getQueryStringArgs(); 
alert(args["q"]); //"javascript" 
alert(args["num"]); //"10" 

2.浏览器取得窗口左边和上边的位置

var leftPos = (typeof window.screenLeft == "number") ? 
 window.screenLeft : window.screenX; 
var topPos = (typeof window.screenTop == "number") ? 
 window.screenTop : window.screenY;

3.取得页面视口的大小

var pageWidth = window.innerWidth, pageHeight = window.innerHeight; 
 
if (typeof pageWidth != "number"){ 
  if (document.compatMode == "CSS1Compat"){ 
    pageWidth = document.documentElement.clientWidth; 
    pageHeight = document.documentElement.clientHeight; 
  } else { 
    pageWidth = document.body.clientWidth; 
    pageHeight = document.body.clientHeight; 
  } 
}

4.检测插件

//检测插件(在 IE 中无效)
function hasPlugin(name){ 
  name = name.toLowerCase(); 
  for (var i=0; i < navigator.plugins.length; i++){ 
    if (navigator. plugins [i].name.toLowerCase().indexOf(name) > -1){ 
      return true; 
    } 
  } 
  return false; 
 } 
 //检测 Flash 
 alert(hasPlugin("Flash")); 
 //检测 QuickTime 
 alert(hasPlugin("QuickTime"));

 //检测 IE 中的插件
 function hasIEPlugin(name){ 
  try { 
    new ActiveXObject(name); 
    return true; 
  } catch (ex){ 
    return false; 
  } 
 } 
 //检测 Flash 
 alert(hasIEPlugin("ShockwaveFlash.ShockwaveFlash")); 
 //检测 QuickTime 
 alert(hasIEPlugin("QuickTime.QuickTime"));

。。。后续

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值