javascript - Using jQuery, how do I pass arg to function and then display a message from predefined variable -
i want call function, pass type of error arg, , display message.
function msgdialog(msg) { // define messages var errormsg = "there has been error. sorry that."; var loginmsg = "something went awry login. please try again."; var uploadmsg = "your upload failed. please try again."; var networkmsg = "you not connected internet. please connect , try again."; alert(msg); }
how call function msgdialog(loginmsg) , have var can assign correct message, that? here alerting it, display differently. know need create new var value of arg value, not sure how. thank you.
that pure javascript, no jquery. try this:
var msgdialog = (function() { var errors = { errormsg : "there has been error. sorry that.", loginmsg : "something went awry login. please try again.", uploadmsg : "your upload failed. please try again.", networkmsg : "you not connected internet. please connect , try again." } return function(msg){ alert(errors[msg]); } })(); msgdialog('uploadmsg'); // alerts "your upload failed. please try again."
you can idea of what's going on here if hadn't seen javascript closures before.
Comments
Post a Comment