1.判断浏览器是否启用cookie:

if (navigator.cookieEnabled==true)
    {
    alert("已启用 cookie")
    }
else
    {
    alert("未启用 cookie")
    }
}

 

1.从url中获取参数的值:

<script type="text/javascript">
    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;
    }
</script>

代码解释:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>js正则表达式</title>
<link rel="stylesheet" type="text/css" href="bootstrap/bootstrap.min.css">
<style type="text/css">
</style>
</head>
<body>

</body>
<script type="text/javascript">
 function getQueryString(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");

    var r = window.location.search.substr(1);
    var result = r.match(reg);

    console.log(r);
    console.log(reg);
    console.log(result);

    if(result!=null){
        return unescape(result[2]);
    }

    return null;

}

var age = getQueryString("age");

console.log(age);

//代码解释:
正则表达式为:/(^|&)age=([^&]*)(&|$)/
分析它匹配的:
^age=10&
^age=10$
&age=10&
&age=10$

待匹配的字符串:
id=123&age=10&sb=1&hu=abc

匹配的结果:
&age=10&
整个&age=10&为第0组; 
第1组为&
第2组为10
第3组为&

</script>
</html>
View Code

相关文章: