js技巧

1. 原生JavaScript判断是否Touch屏幕
</pre><pre code_snippet_id="252965" snippet_file_name="blog_20140324_1_7059274" name="code" class="html">function isTouchScreen(){
        return (('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch);
}

2.自动加载js文件

if (!((window.DocumentTouch && document instanceof DocumentTouch) || 'ontouchstart' in window)) {
            var script = document.createElement("script");
            script.src = "plugins/af.desktopBrowsers.js";
            var tag = $("head").append(script);
        }

3.用正则和document .location.search 获取URL的值

  要获取当前页面URL的参数,可能大家第一个想到是使用 window.location.href 或者是document.location.href ,获取结果诸如http://www.xxx.com/?aa=xx&bb=xx ;但是其实我们需要的只是:?aa=xx&bb=xx。这种形式可以使用 document.location.search 这个属性获取。

如果我想要获取该URL后面参数aa的值该怎么弄呢?常见的方式可能是这样:

复制代码
  
  
function ( param ){
var url = window . location . toString ();
url
= url . split ( ' ? ' );
if ( typeof (url [ 1 ]) == ' string ' ) {
url
= url [ 1 ]. split ( ' & ' );
for (i = 0 ;i < url . length ;i ++ )
{
s
= url [ i ]. split ( " = " );
if ( s[ 0 ] == " param " ) return s[ 1 ];
}
}
return null ;
}
复制代码

改用document.location.search和正则获取参数将使代码更加简洁:

复制代码
  
  
function getParameter (sProp )
{
var re = new RegExp (sProp + " =([^\&]*) " , " i " );
var a = re . exec (document . location . search );
if (a == null )
return null ;
return a [ 1 ];
};
复制代码

4. $.ui.useOSthemes=false的理解

点击打开链接

5. $.ui.autoLaunch=false的理解

点击打开链接




添加 20140612

采用正则表达式获取地址栏参数:( 强烈推荐,既实用又方便!)

function  GetQueryString(name)
{
      var  reg = new  RegExp( "(^|&)" + name + "=([^&]*)(&|$)" );
      var  r = window.location.search.substr(1).match(reg);
      if (r!= null ) return   unescape(r[2]); return  null ;
}
 
// 调用方法
alert(GetQueryString( "参数名1" ));
alert(GetQueryString( "参数名2" ));
alert(GetQueryString( "参数名3" ));



添加20140710

js获取当前域名

<script language="javascript">
//获取域名
host = window.location.host;
host2=document.domain; 

//获取页面完整地址
url = window.location.href;


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值