check if a certain query string is set then apply it to all url's on the page

this script will check if a query string is already set, and if it matches it will append the same query string to all url's on the page

Here is the test link with the field, and all url's on this page should now also have the query string myfield=true if this is not set then all url's will not have this query string.

With myfield=true 

Without myfield=true 


$(document).ready(function() {
  var field = 'myfield';
  var url = window.location.href;
  if(url.indexOf('?' + field + '=') != -1) {
  	$('a').each(function() {
      	var href = $(this).attr('href');
      	if (href) {
          	href += (href.match(/\?/) ? '&' : '?') + 'myfield=true';
          	$(this).attr('href', href);

Other Items in javascript

No Items Found.

Add Comment
Type in a Nick Name here
Page Views

This page has been viewed 268 times.

Search Code
Search Code by entering your search text above.

This is my test area for webdev. I keep a collection of code snippits here, mostly for my reference. Also if i find a good site, i usually add it here.


Random Quote
therock Somewhere along this crazy road I learned (often times the hard way) the most important things I can do is be authentic, trust my gut, be the hardest worker in the room, celebrate the mistakes, be a grateful man and always remember that it's nice to be important, but it's more important to be nice.
The Rock