欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

获取地址栏参数

程序员文章站 2024-02-17 22:20:34
...

/*

功能:获取地址栏参数
使用:getQueryString('str')
代码:this.$getQueryString("order_id");//001002
代码:this.$getQueryString("order_title");//123

http://localhost:9010/index.html?order_id=001002&order_title=123&total_amount=1.99
*/
js:

function getQueryString(name) {
  var search = document.location.href;
  var pattern = new RegExp("[?&]" + name + "\=([^&]+)", "g");
  var matcher = pattern.exec(search);
  var items = null;
  if (null != matcher) {
    try {
      items = decodeURIComponent(decodeURIComponent(matcher[1]));
    } catch (e) {
      try {
        items = decodeURIComponent(matcher[1]);
      } catch (e) {
        items = matcher[1];
      }
    }
  }
  return items;
}

vue:

main.js/

import { getQueryString } from '../static/js/common'
Vue.prototype.$getQueryString = getQueryString
comm.js//
//地址栏获取参数
export const getQueryString = function(name){
  var search = document.location;
  var pattern = new RegExp("[?&]" + name + "\=([^&]+)", "g");
  var matcher = pattern.exec(search);
  var items = null;
  if (null != matcher) {
    try {
      items = decodeURIComponent(decodeURIComponent(matcher[1]));
    } catch (e) {
      try {
        items = decodeURIComponent(matcher[1]);
      } catch (e) {
        items = matcher[1];
      }
    }
  }
  return items;
}
相关标签: 获取地址栏参数