Function for getting url parameter with hash or search tag

November 22, 2014
by Inaam Hussain

This tutorial is for how to get url parameter whether its in hash tag or in search tag.

so, for this we have custom function which will fetch all the parameter whether it’s in hash tag or search tag. and store that parameter in “config” object. so based on that we can use it for different further process.

The function is,

 

function getLinkParameters(){
var config = {};
if(location.search){
 addToModel(location.search.slice(1).split('&'));
}
if(location.hash){
 addToModel(location.hash.slice(1).split('&'));
}
function addToModel(searchParams){
 for(var s in searchParams){
  var q = searchParams[s].split('=');
   config[q[0]] = !!q[1] ? unescape(q[1]) : "";
  }
 }
}

 

So, how getLinkParameters function work?

Firstly created a object for storing url parameter values.

and then have 2 condition that will check whether it’s “Hash” or “Search”.

and that values pass to “addToModel” function with slice and split by “&”.

addToModel function check that parameter and split with “=” and stored that value in config object.

now we have all the parameter in config object.

how to update hash tag values in url by changing specific site data

 

this function is used for update hash tag in url by changing specific site data,

(ex:- While change tab in site that will reflect in url).

 

function updateHashlink(data){
 var params = [];
 params.push("data="+data);
 params = params.join('&');
 location.hash = params;
 return params;
}

 

So, how updateHashlink function work?

 

By calling this function firstly created a temporary “param” array which will store param values.

add data to params by using push function.

and then will update hash by using “location.hash = params”.

 

About

Engineer. Blogger. I like to be updated on new upcoming emerging technologies.

Get notified on our new articles

Subscribe to get the latest on Node.js, Angular, Blockchain and more. We never spam!
First Name:
Email:

Leave a Comment