lundi 20 juin 2016

Javascript isnull


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?


Aucun commentaire:

Enregistrer un commentaire