1
\$\begingroup\$

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='&mapControl=true'}
    thisUrl=thisUrl+param;
} else {
    $('#urlParam').val(thisUrl);
    if (thisUrl.indexOf('?mapControl=true') >= 0){
        thisUrl=thisUrl.replace('?mapControl=true',''); 
    } else if (thisUrl.indexOf('&mapControl=true') >= 0){
        thisUrl=thisUrl.replace('&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='&'}
    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('&mapControl=true',''); 
    }
    urlParamObj.val(thisUrl);
});

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

\$\endgroup\$
1
\$\begingroup\$

This is tighter I think:

function addParameter( url , name , value )
{
  var justHostname = ( location.href == ( location.protocol + "//" + location.host + "/" ),
      separator = justHostname?"?":"&"
  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&','?')
               .replace('?mapControl=true','')
               .replace('&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

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.