zoukankan      html  css  js  c++  java
  • 巧用正则和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 ];
    };
  • 相关阅读:
    type() & dir()

    手机操作API
    APP模拟手势高级操作
    APP元素事件操作API
    APP元素信息操作API
    APP元素定位操作
    手机控件查看工具uiautomatorviewer
    App基础操作API
    Appium入门
  • 原文地址:https://www.cnblogs.com/codebean/p/2059901.html
Copyright © 2011-2022 走看看