• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 150
  • Last Modified:

What Does This JS Code Do?


I had a developer work on this code, not quite sure what it does as I can't talk to him anymore. This is on the navigation area of a website:

<script type="text/javascript">
  function getAbsolutePath() {
  var loc = window.location;
  var pathName = loc.pathname.substring(0, loc.pathname.lastIndexOf('/') + 1);
  return loc.href.substring(0, loc.href.length - ((loc.pathname + loc.search + loc.hash).length - pathName.length));

  $().ready(function () {
  if ($.browser.msie && $.browser.version == 10) {
  var path =  window.location.pathname;
  if (path.substr(0,1) == '/'){
  path = path.substring(1, path.length);
  if(path == ''){path = 'index.php'}
  var links = $('#menu').find('a');
  links.each(function(index) {
  if ($(this).attr('href') == path){
  if ( $(this).parent().hasClass('first')){


Open in new window

Computer Guy
Computer Guy
1 Solution
Seems to be parsing the URL to see what menu item is selected and then applying on of two classes to it and everything else.
Ken ButtersCommented:
looks to me like it is executing when you first load your webpage.

Step 1 : resolve current page of the document.

if it cannot resolve the path of the webpage, it will use 'index.php' by default, and it will assign that webpage link address to path variable.

Step 2 : Loop through all of the links found inside the element id "menu", and then comparing the href attribute to the path of the current page of the document.

When the href attribute of the link inside the menu id class is equal to the path of the document, it will add a class to the element.  

if the element already has a class of first, it will add 'hover-first'
otherwise it will add a class of 'hover'.

I assume the overall intent is to add CSS styling to the hyperlinks of the menu items.
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.

Join & Write a Comment

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now