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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jason C. LevineDon't talk to me.Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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. LevineDon't talk to me.Commented:
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. LevineDon't talk to me.Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Internet / Email Software

From novice to tech pro — start learning today.