When I add a query parameter to a URL in the address bar, how do I retrieve it from JavaScript or jQuery? For example, when I enter index.php?id=70&edid=33, how do I retrieve the value 33?
            Asked
            
        
        
            Active
            
        
            Viewed 442 times
        
    0
            
            
         
    
    
        Sasha Goldshtein
        
- 3,499
- 22
- 35
 
    
    
        Mahdi Bahari
        
- 109
- 1
- 10
- 
                    1Rewrote the question to clarify what is being asked. – Sasha Goldshtein Jul 23 '15 at 12:14
- 
                    1Possible duplicate of [How can I get query string values in JavaScript?](https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – clickbait Jul 27 '18 at 22:07
3 Answers
1
            
            
        In modern browsers, you may use a URL.searchParams [↗] object.
// Create a new URL object
let address = new URL(window.location);
// Get searchParameters property of the URL object
let queryParameters = address.searchParams;
// Retrieve specific query parameters
let id = queryParameters.get("id"); // 70
let edid = queryParameters.get("edid"); // 33
Browser support for URL.searchParams [↗] is as follows:
- Edge 17+
- Chrome 49+
- Safari 10.1+
- Firefox 44+
 
    
    
        clickbait
        
- 2,818
- 1
- 25
- 61
- 
                    1Be careful with this if you are using base64 parameters. The "+" character is returned as " " space. A regex replace will fix it as queryParameters.get("name").replace(/ /g, "+"); – Keith Thomas Feb 09 '21 at 02:41
0
            
            
        here is my answer for you. I think this is the best solution for this question. You can replace "index.php?id=70&edid=33" to "window.location.pathname" if the pathname contains the your deserve parameter.
var url="index.php?id=70&edid=33";
var qString=url.split("?")[1];
//console.log(qString);
var param_val=qString.split("&");
var params=[];
for(var i=0; i < param_val.length; i++){
  params.push(param_val[i].split("="));
}
var edit_id;
for(var j=0; j < params.length; j++){
    if(params[j][0]==="edid"){
      edit_id=params[j][1];
      break;
     }
}
console.log(edit_id);-1
            
            
        You can get complete url from address bar via javascript and then manipulate it as follow
var address=document.location;
var edid=address. substring(address.indexOf('edid')+5, address.indexOf(edid)+7);
This is not a perfect solution for this problem, but it will give you a idea of getting stuff to work.
 
    
    
        warlock-sangrur
        
- 79
- 3