2
console.log(_GET("appid"));

在 fucn _GET 我需要检查参数是否存在,如果存在则返回它。

function _GET(paramName) {
    var hash = window.location.hash; // #appid=1&device&people=

    //this needs to be not static
    if (/appid=+\w/.test(window.location.hash)) {
        //and somehow parse it and return;
    }
    return false;
}

我希望在控制台中看到 1,如果我 console.log(_GET("device")) 或 people 然后为 null

4

3 回答 3

0
function _GET(paramName) {
    var hash = window.location.hash.match(new RegExp("appid=+\w", 'gi')); // #appid=1&device&people=

    //this needs to be not static
    if (hash) { 
        //and somehow parse it and return;
    }
    return false;
}
于 2013-07-31T12:16:10.540 回答
0

您需要使用String.match并传入RegExp对象:

function _GET(paramName) {
    var pattern = paramName + "=+\w";
    return (window.location.hash.match(new RegExp(pattern, 'gi'))) != null;
}
于 2013-07-31T12:17:16.017 回答
0
import params from './url-hash-params.mjs';

// example.com/#city=Foo&country=Bar
const { city, country } = parms;

url-hash-params.mjs

export default (function() {
  const params = new Map();

  window.addEventListener('hashchange', updateParams);
  updateParams();

  function updateParams(e) {
    params.clear();
    const arry = window.location.hash.substr(1).split('&').forEach(param => {
      const [key, val] = param.split('=').map(s => s.trim());
      params.set(key, val);
    });
  }

  return new Proxy(params, {
    get: (o, key) => o.get(key)
  });
})();
于 2019-12-30T15:05:14.740 回答