Javascript Add query string parameter dynamically to some or all links on the current page - Code-Tips.com - Web Development, Programming, SEO

Sunday, February 13, 2011

Javascript Add query string parameter dynamically to some or all links on the current page

This Javascript function retrieves all <a> tags (hyperlinks) from the current page, and updates each link to include the supplied query string parameter and value.
The third (optional) parameter passed to the function is an array of strings containing the domains that should be included when updating links on the page.
If a link points to a domain that is not in the array passed to the function, the link will remain unchanged.

If no Array of domains to include is supplied when calling the function, only the current domain is used.  For each link on the current page that the function loops through, the query string parameter is added to the link if the host matches one of the hosts in the array of domains.


function updateLinks(parameter, value, includeDomainsArray)
{

   var links = document.getElementsByTagName('a');
   var includeDomains = new Array();
   
   if(arguments.length == 3) //has include domains
   {
       //Links will be updated only if they point to one of the following domains:
       //includeDomains = includeDomainsArray.split("|");
       includeDomains = includeDomainsArray;
   }
   else
   {
       //Links will be updated only if they point to one the current domain only:
       includeDomains[0] = self.location.host;
   }
   
   for (var i=0;i<links.length;i++)
   {
       if(links[i].href != "#" && links[i].href != "/" && links[i].href != "" && links[i].href != window.location) //Ignore links with empty src attribute, linking to site root, or anchor tags (#)
       {
           var updateLink = false;
           for(k=0;k<includeDomains.length;k++)
           {
               if(links[i].href.toLowerCase().indexOf(includeDomains[k].toLowerCase()) != -1) //Domain of current link is inlcluded i the includeDomains array.  Update Required...
                   updateLink = true;
           }
       
           if(!updateLink)
           {
               //Do nothing - link not is includeDomains array
           }
           else
           {
               var queryStringComplete = "";
               var paramCount = 0;
           
               var linkParts = links[i].href.split("?");
               
               if(linkParts.length > 1) // Has Query String Params
               {
                   queryStringComplete = "?";
               
                   var fullQString = linkParts[1];
                   var paramArray = fullQString.split("&");    
                   var found = false;
                   
                   for (j=0;j<paramArray.length;j++)
                   {
                       
                       var currentParameter = paramArray[j].split("=");
                       
                       if(paramCount > 0)
                           queryStringComplete = queryStringComplete + "&";
                       
                       if(currentParameter[0] == parameter) //Parameter exists in url, refresh value
                        {
                            queryStringComplete = queryStringComplete + parameter + "=" + value;
                            found = true;
                        }
                        else
                        {
                            queryStringComplete = queryStringComplete + paramArray[j]; //Not related parameter - re-include in url
                        }
                       
                        paramCount++;
                   }
                   
                   if(!found) //Add new param to end of query string
                       queryStringComplete = queryStringComplete + "&" + parameter + "=" + value;
               }
               else
               {
                   queryStringComplete = "?" + parameter + "=" + value;
               }
                   
               links[i].href = links[i].href.split("?")[0] + queryStringComplete;        
           }
       }
       else
       {
           //Do nothing - link not is includeDomains array
       }
   }
}

Previous: Javascript Return the url of the current page with no query string parameters


Javascript Query String Manipulation
(TOC)

1 comment:

  1. Hi I am very new at this, I want to use your script to add ?id=abc to all urls on my page. What changes do I need to make to get this done?

    ReplyDelete

Understood
This website is using cookies. More details