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.

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Aug 29, 2013 at 18:16
\$\endgroup\$

1 Answer 1

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

answered Aug 29, 2013 at 18:57
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.