javascript - Different "return null" results in Chrome and Firefox -
given following code:
jquery(document).ready(function($) { $('[name="clickabletd"]').click(function() { window.document.location = $(this).attr("href"); }); }); function add(id) { var stock = "2"; var quantity = prompt("select quantity:", 1); if (quantity == null) { return null; } else if (quantity <= stock) { alert("good"); } else if (quantity > stock) { alert("over"); } else if (isnan(quantity)) { alert("nan"); } }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <table border="1"> <tr> <td name="clickabletd" href="javascript:add('1')"> <a>click</a> </td> </tr> </table>
if user returns null value (by clicking cancel) using chrome browser nothing happens expected.
but if user using firefox redirected url add() , display null text string on screen.
why firefox react differently chrome?
i've been having trouble finding detailed specification of how javascript:
urls work, , they're supposed when code returns other string or undefined. looks chrome , firefox have taken different approaches in case. chrome treats them undefined
, , doesn't change page contents. ff various things: if returns null
displays string null
; if returns object single property, displays value of property.
use return void(0)
or return undefined
instead of null
better compatibility between browsers.
Comments
Post a Comment