How to get URL parameter using jQuery or plain JavaScript?

前端 未结 30 3244
天涯浪人
天涯浪人 2020-11-21 06:29

I have seen lots of jQuery examples where parameter size and name are unknown.

My URL is only going to ever have 1 string:

http://example.com?sent=ye         


        
30条回答
  •  心在旅途
    2020-11-21 07:08

    function GetRequestParam(param)
    {
    	var res = null;
    	try{
    		var qs = decodeURIComponent(window.location.search.substring(1));//get everything after then '?' in URI
    		var ar = qs.split('&');
    		$.each(ar, function(a, b){
    			var kv = b.split('=');
    			if(param === kv[0]){
    				res = kv[1];
    				return false;//break loop
    			}
    		});
    	}catch(e){}
    	return res;
    }

提交回复
热议问题