I have these two functions.
The first is well written because someone who knows how to write js did it.
The second I did.
var GetURLParameter = function($param){
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
for(var i = 0; i < sURLVariables.length; i++) {
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == $param) {
return sParameterName[1];
}
}
}
if(GetURLParameter('filter')!= "undefined"){
$('#'+GetURLParameter('filter')).parent().parent().children('li.active').removeClass('active')
$('#'+GetURLParameter('filter')).parent().addClass('active');
}
I would like to simplify the calls as I’m repeating the line GetURLParameter('filter') but I don’t know how I can make it work like the key word this
Just stash the intermediate values in variables:
Of course this has the advantage of being more readable, and also it’s more efficient because it doesn’t re-rerun the same function or query the DOM multiple times.