Posts

Showing posts from February, 2011

Javascript Query String Manipulation Functions

Javascript Query String Manipulation Functions The following is a series of articles with Javascript functions that can be used to manipulate the query string parameters and variables in the current url.  Use the links below to go to a specific function: Javascript Add Query String Parameter Add a query string parameter to the url of the current page. Javascript Check if Query String Parameter Exists in current URL Test if the query string of the current URL contains a specific parameter. Javascript Remove a parameter from the query string if found in current url Remove a parameter from the query string of the current page. Javascript Get the value of the specified Query String Parameter Find the value of a specific query string parameter in the current URL.  Return the value of the parameter, or false if not found. Javascript Get all parameters and values as an Array Returns a multidimensional array containing parameters and values from the query string ...

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.

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

This Javascript function strips/removes any existing query string parameters from the url and returns the 'cleaned' url.

Javascript Get all parameters and values as an Array

This Javascript function extracts each parameter from the query string and returns a multi dimensional array containing the parameter names and values. Array( Array(parameter_name1, parameter_value1) , Array(parameter_name2, parameter_value2) )

Javascript Get the value of the specified Query String Parameter

This Javascript function checks the address of the current page for the supplied query string parameter.  If found, the value of the parameter is used, if the parameter is not found, false is returned. See also:  checkParemeterExists() , which returns true or false if the specified parameter is found or not.

Javascript Remove a parameter from the query sring if found in current url

This Javascript function removes the supplied parameter from the query string if it is present in the current url.  This is done by converting the query string into an array of parameters and values, then rebuilding the final string to exclude the parameter supplied to the function.  The browser is then redirected to the current page with the new query string.

Javascript Check if Query String Parameter Exists in current URL

This Javascript function checks if the parameter supplied exists in the query string.  If the parameter supplied is found in the query string of the current URL, the function will return true.  If the parameter is not found, false is returned. See also:   getParameter() - This function returns the value of the parameter if found, and false if it is not found which can be used instead of the checkParameterExists() function below

Javascript Add Query String Parameter

Redirect to the current page including the specified query string parameter and value This Javascript function redirects the browser back to the current page with the query string parameter data included that is passed to the function.  If the current url already contains the supplied query string and value, no redirect will take place.  Existing parameters and values are retained in the url after the redirect. This function can be useful when a common query string parameter is required across multiple pages on a site, such as a session hash/id or similar.   The current page will reload to include the specified parameter and value if it doesn't already exist.  Use this function in combination with the  updateLinks() function to add and manage query string parameters and variables across pages on a site dynamically.