javascript - Set default url parameter value when none is specified -
i found answer getting url arguments javascript on internet:
function geturlparameter(name) { return decodeuricomponent((new regexp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null }
i wondering if there way modify this, when no url arguments specified, preset value become argument value.
eg. index.html?value=1
return 1,
if set default value 2, , types index.html
variable value 2.
add argument specify default value if key missing:
function geturlparameter(name, _default) { var value = decodeuricomponent((new regexp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20')) || null return value !=null ? value : _default; }
(fyi implementation treats ;
delimiter: k=a;b
return a
, bit odd.)
Comments
Post a Comment