This is a really great function written in jQuery to determine the value of a url field:
$.urlParam = function(name){
var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
return results[1] || 0;
}
// example.com?someparam=name&otherparam=8&id=6
$.urlParam('someparam'); // name
$.urlParam('id'); // 6
$.urlParam('notavar'); // null
http://snipplr.com/view/11583/retrieve-url-params-with-jquery/
I would like to add a condition to test for null, but this looks kind of klunky:
if (results == null) {
return 0;
} else {
return results[1] || 0;
}
Q: What's the elegant way to accomplish the above if/then statement?
return results == null ? 0 : (results[1] || 0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With