zoukankan      html  css  js  c++  java
  • js 获取 url 参数

     

    /**
     * 根据页面地址获取所有参数对象
     * @return Object{} 返回所有参数
     * ------------------------------
     * 根据页面地址获取指定参数对象
     * @return String 返回指定参数
     * ------------------------------
     * 示例: http://xxx/x.json?name=test&id=100
     * $.getPageParams() --> { name:"test", id:"100" }
     * $.getPageParams("name") --> "test"
     * $.getPageParams("id") --> "100"
     * ------------------------------
     */
    $.getPageParams = function(name){
    	var params = {};
    	var string = window.location.search.substr(1);
    	if(string) {
    		var array = string.split('&');
    		$.each(array, function(idx, text){
    			var words= text.split('=');
    			params[words[0]] = decodeURIComponent(words[1]);
    		});
    	}
    	return name == null ? params : params[name];
    };
    

      

    // 采用正则表达式获取地址栏参数
    function GetQueryString(name)
    {
         var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
         var r = window.location.search.substr(1).match(reg);
         if(r!=null)return  decodeURIComponent(r[2]); return null;
    }

    window.location 

     console.log(window.location.href);
     console.log(window.location.protocol +"//"+ window.location.hostname +":"+ window.location.port + window.location.pathname);
  • 相关阅读:
    jQuery知识点
    mysql基本命令
    正则表达式
    vue跨域解决方法
    字符串反转
    两个数组的交集
    删除排序数组中重复项
    缺失的第一个正整数
    275. H 指数 II
    274. H 指数
  • 原文地址:https://www.cnblogs.com/ooo0/p/6744030.html
Copyright © 2011-2022 走看看