Skip to main content
Code Review

Return to Revisions

2 of 2
replaced http://stackoverflow.com/ with https://stackoverflow.com/

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

konijn
  • 34.3k
  • 5
  • 70
  • 267
default

AltStyle によって変換されたページ (->オリジナル) /