How does this javascript work? What does it mean?

See this page:

http://freshservice.com/compare-it-service-desk

There is a heading:

IT SERVICE DESK SOFTWARE

This is the sub-heading:

Why Freshservice is better than other Service Desks


This is a landing page for a Google adwords campaign.

We have a script on this page that pulls in the keyword that the user searched (Google adwords API) and populates it in the URL and then takes that KW and makes a change on the page.

For eg:

If someone searches for sysaid and sees/clicks on the ad and gets to the landing page, we see this:

http://freshservice.com/compare-it-service-desk?kw=sysaid

This is the heading:

SYSAID VS FRESHSERVICE

This is the sub-heading:

Why Freshservice is better than Sysaid


This is the code that was written by the developer and is used:

Could someone please explain to me what this code means and how it works?


<script type="text/javascript">
function getParam ( sname )
{
  var params = location.search.substr(location.search.indexOf("?")+1);
  var sval = "";
  params = params.split("&");
    for (var i=0; i<params.length; i++)
       {
         temp = params[i].split("=");
         if ( [temp[0]] == sname ) { sval = temp[1]; }
       }
  return sval;
}



function process_keyword (sname)
{
  var apps = {};
  apps["servicenow"]="ServiceNow";
apps["service now"]="ServiceNow";
apps["cherwell"]="Cherwell";
apps["landesk"]="Landesk";
apps["connectwise"]="Connectwise";
apps["sysaid"]="Sysaid";
apps["track it"]="Track-It";
apps["ibm tivoli"]="IBM Tivoli";
apps["ca unicenter"]="CA Unicenter";
apps["frontrange heat"]="FrontRange Heat";
apps["frontrange"]="FrontRange";
apps["servicedesk plus"]="ServiceDesk Plus";
apps["autotask"]="Autotask";
apps["bmc remedy"]="BMC Remedy";
apps["bmc remedyforce"]="BMC RemedyForce";
apps["samanage"]="Samanage";
apps["hp service manager"]="HP Service Manager";
apps["topdesk"]="TopDesk";
apps["dell kace"]="Dell Kace";
apps["dellkace"]="DellKace";         
apps["manageengine"]="ManageEngine";
apps["citrix gotoassist"]="Citrix GoToAssist";
apps["jira service desk"]="JIRA Service Desk";
apps["numara footprints"]="Numara Footprints";
apps["zendesk for it"]="Zendesk for IT";
  keyword = unescape(getParam(sname));
  for (key in apps){
    if (keyword.toLowerCase().contains(key)){
      return apps[key];
    }
  }
  return "other Service Desks";
}

function set_data(sname){
  $('#replace_text').html("Why Freshservice is better than "+process_keyword(sname));
  var keyw = process_keyword(sname);
  if( keyw == "other Service Desks"){
  	$('#replace_text1').html("IT SERVICE DESK SOFTWARE");  
  }
  else{
    $('#replace_text1').html(process_keyword(sname).toUpperCase()+" Vs FRESHSERVICE");  
  }
  
}
</script>

Open in new window

smuralisankarAsked:
Who is Participating?
 
Jason C. LevineNo oneCommented:
The following section gets the search term:

function getParam ( sname )
{
  var params = location.search.substr(location.search.indexOf("?")+1);
  var sval = "";
  params = params.split("&");
    for (var i=0; i<params.length; i++)
       {
         temp = params[i].split("=");
         if ( [temp[0]] == sname ) { sval = temp[1]; }
       }
  return sval;
}

Open in new window


This section compares the search term against a defined list of terms:

  keyword = unescape(getParam(sname));
  for (key in apps){
    if (keyword.toLowerCase().contains(key)){
      return apps[key];

Open in new window


This section writes the output.  If there is no match from the previous step, it writes a generic "other Service Desks"

function set_data(sname){
  $('#replace_text').html("Why Freshservice is better than "+process_keyword(sname));
  var keyw = process_keyword(sname);
  if( keyw == "other Service Desks"){
  	$('#replace_text1').html("IT SERVICE DESK SOFTWARE");  
  }
  else{
    $('#replace_text1').html(process_keyword(sname).toUpperCase()+" Vs FRESHSERVICE");  
  }
  
}
</script>

Open in new window

0
 
smuralisankarAuthor Commented:
Thanks a lot.

I am not an expert developer but I am trying to understand how this works and make modifications and maybe take this further.

I spent quite a bit of time search online but i only got bits and pieces here and there.

I am interested in learning about this at a deeper level. If I could get a more detailed (step by step) description of how this code works, I would really appreciate it.
0
 
Jason C. LevineNo oneCommented:
I'm not able to take the time to break it down line by line and function by function for you.  You'll need to go through it yourself, looking up the various commands on Google as you go.  Post new questions here (on EE, not this specific question) if you get stuck.
0
 
smuralisankarAuthor Commented:
function getParam ( sname )
{
  var params = location.search.substr(location.search.indexOf("?")+1);
  var sval = "";
  params = params.split("&");
    for (var i=0; i<params.length; i++)
       {
         temp = params[i].split("=");
         if ( [temp[0]] == sname ) { sval = temp[1]; }
       }
  return sval;
}

Open in new window



I understand that this function:
location.search.substr

Will return the query parameter as a string.

location.search.indexOf
Not sure what this does. I can't find a good answer.

To be honest, I can't understand the the rest of this snippet.
0
 
Jason C. LevineNo oneCommented:
http://www.w3schools.com/jsref/jsref_indexOf.asp

indexOf searches a string for some text.

var params = location.search.substr(location.search.indexOf("?")+1);

The above translates to: Set the variable "params" to be everything in the URL after the ?.  In other words, the query string.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.