Quantcast
Channel: Adobe AEM CQ tips » javascript
Viewing all articles
Browse latest Browse all 6

Read hash from URL as key-value pairs using javascript

$
0
0

Problem

How to get key-value pairs from hash(location.hash) part of URL via javascript?

Solution

function parseHashArgs(aURL) {
	 
	aURL = aURL || window.location.href;
	
	var vars = {};
	var hashes = aURL.slice(aURL.indexOf('#') + 1).split('&');
 
    for(var i = 0; i < hashes.length; i++) {
       var hash = hashes[i].split('=');
      
       if(hash.length > 1) {
    	   vars[hash[0]] = hash[1];
       } else {
     	  vars[hash[0]] = null;
       }      
    }
 
    return vars;
}

Viewing all articles
Browse latest Browse all 6

Trending Articles