Determine whether a checkbox is checked or unchecked I have a jQuery "checkbox checked/unchecked" function that works well. This is a checkbox for turning a particular URL parameter on or off.  But I believe this code could be written a lot tighter. Does anyone have any suggestions?

$('#mapControl').live('click', function(){ 
var thisUrl = $(location).attr('href');
if($(this).is(':checked')) {
    var lastFour = thisUrl.substr(thisUrl.length - 4);
    var param;
    if (lastFour == 'com/') {param='?mapControl=true'} else {param='&amp;mapControl=true'}
    thisUrl=thisUrl+param;
} else {
    $('#urlParam').val(thisUrl);
    if (thisUrl.indexOf('?mapControl=true') &gt;= 0){
        thisUrl=thisUrl.replace('?mapControl=true',''); 
    } else if (thisUrl.indexOf('&amp;mapControl=true') &gt;= 0){
        thisUrl=thisUrl.replace('&amp;mapControl=true',''); 
    } 
}
$('#urlParam').val(thisUrl);
});


I was given some suggestions (because i erroneously posted on StackOverflow) and was asked to post my question here. So with the feedback I got - I ended up with this code.

    function getParam() {
    var n;
    var myUrl = window.location.href;
    var lastFour = myUrl.substr(myUrl.length - 4);
    if (lastFour == 'com/') {n='?'} else {n='&amp;'}
    return n;
}

$('#mapControl').on('click', function(){ 
    var thisUrl = window.location.href;
    var urlParamObj = $('#urlParam');
    if(this.checked === true) {
        var lastFour = thisUrl.substr(thisUrl.length - 4);
        var param = getParam();
        thisUrl=thisUrl+param+'mapControl=true';
    } else {
        urlParamObj.val(thisUrl);
        thisUrl=thisUrl.replace('?mapControl=true','').replace('&amp;mapControl=true',''); 
    }
    urlParamObj.val(thisUrl);
});


I think this is the tightest it could be and I can use the getParam function for other checkboxes.
This is tighter I think:

function addParameter( url , name , value )
{
  var justHostname = ( location.href == ( location.protocol + "//" + location.host + "/" ),
      separator = justHostname?"?":"&amp;"
  return url + separator + encodeURIComponent(name) + "=" + encodeURIComponent(value);
}


$('#mapControl').on('click', function(){ 
    var url = window.location.href,
        urlParamObj = $('#urlParam');
    if(this.checked) 
    {
      url = addParameter( url , 'mapControl' , 'true' )
    } else {
      url = url.replace('?mapControl=true&amp;','?')
               .replace('?mapControl=true','')
               .replace('&amp;mapControl=true',''); 
    }
    urlParamObj.val(thisUrl);
});


I did not test this, but you should catch the drift.

If it were me though, I would not re-invent the wheel and use the code from the answer here : 
https://stackoverflow.com/questions/6953944/how-to-add-parameters-to-a-url-that-already-contains-other-parameters-and-maybe