html - Function Return Behavior in Javascript -
i have <a href='...'>link</a>
want invoke custom warning if click , either select "ok" or "cancel" button underlying in html taking appropriate action. default action if "ok" or nothing if "cancel."
the code in html code is:
<div id="dialogoverlay"></div> <div id="dialogbox"> <div> <div id="dialogboxhead"></div> <div id="dialogboxbody"></div> <div id="dialogboxfoot"></div> </div> </div> <a href='sumcodehere' return onclick=\"javascript: return warn.render(\'this operation cannot reversed.\')\"><img></a>
the javascript code follows:
<script> function warn() { this.render = function(dialog) { var winw = window.innerwidth; var winh = window.innerheight; var dialogoverlay = document.getelementbyid('dialogoverlay'); var dialogbox = document.getelementbyid('dialogbox'); dialogoverlay.style.display = "block"; dialogoverlay.style.height = winh+"px"; dialogbox.style.left = (winw/2) - (550 * .5)+"px"; dialogbox.style.top = "300px"; dialogbox.style.display = "block"; document.getelementbyid('dialogboxhead').style.background = "url('../images/warninghead.gif') no-repeat"; document.getelementbyid('dialogboxhead').style.height = "30px"; document.getelementbyid('dialogboxbody').style.background = "#fbfab9"; document.getelementbyid('dialogboxbody').innerhtml = dialog; document.getelementbyid('dialogboxfoot').style.background = "#fbfab9"; document.getelementbyid('dialogboxfoot').innerhtml = '<button onclick="return warn.ok(); return false">go ahead , delete</button> <button onclick="return warn.cancel(); return true">get me out of here!</button>'; return false; } this.ok = function() { document.getelementbyid('dialogbox').style.display = "none"; document.getelementbyid('dialogoverlay').style.display = "none"; return true; } this.cancel = function() { document.getelementbyid('dialogbox').style.display = "none"; document.getelementbyid('dialogoverlay').style.display = "none"; return false; }
i'm trying in firefox, , behavior being exhibited either button brings warning dialog, both buttons, when clicked, seem returning "false."
at first html needs fixed:
<a href="sumcodehere" onclick="return warn.render('this operation cannot reversed.')"> <img src="..." alt="..." /> </a>
(removed unknown return
attribute , fixed quoting of onclick
attribute.)
then in js, warn()
looks constructor function, use such, , create global object:
var warn = new warn();
notice small letter @ beginning of variable name. same variable used in inline handler.
a better way attach event listeners elements use addeventlistener
method, though.
Comments
Post a Comment