Thursday, 15 May 2014

javascript - Issue in conditionally preventing the default action of anchor tag -



javascript - Issue in conditionally preventing the default action of anchor tag -

there anchor tag: inventoryhome takes user different page.

if size of ratemap greater 0, need show confirmation pop-up confirming if user wants discard unsaved changes.

the method navigatetoinv() called if user confirms discard.

in code below, though navigatetoinv() method called & click event getting triggered, page not navigating new page.

<li><a href="<%=request.getcontextpath()%>/inventory" id="inventoryhome"> <spring:message code="inventorytab" /></a></li> $('#inventoryhome').click(function(e) { if (object.size(ratemap) > 0) { e.preventdefault(); showdiscardconfirmationpopup(navigatetoinv, false); } }); var navigatetoinv = function (){ ratemap = new object(); $('#inventoryhome').trigger('click'); } object.size = function(obj) { var size = 0, key; (key in obj) { if (obj.hasownproperty(key)) size++; } homecoming size; };

not able identify issue.

the problem calling trigger('click') on anchor tag not cause navigate page specified href value. see this answer.

you either have set window.location href value, or alter logic phone call e.preventdefault() after user has chosen not discard changes, this:

$('#linkid').click(function(e) { if (hasunsavedchanges() && !confirmdiscard()) { e.preventdefault(); } }

in case be:

$('#inventoryhome').click(function(e) { if (!$.isemptyobject(ratemap) && !confirm("discard changes?")) { e.preventdefault(); } }

javascript jquery

No comments:

Post a Comment