javascript - jQuery load script executes many times -
i have simple method loads content , should replace div element:
$("a[data-ajax='true']").on('click', function (event) { event.preventdefault(); var gotolink = this.href; animatelink(this); $('#ajax-target').fadeto(0.2, 0.5, function () { lastlink = currentlink; currentlink = gotolink; $('#ajax-target').load(gotolink, {}, function () { $('body').scrolltop(0); $('#ajax-target').fadeto('slow', 1); }); }); });
now load per ajax html code contains following code:
<script> console.log('running...') </script>
first time click on link loads ajax, see 'running...' on console expected. second time so, 'running...' printed twice on console. third time message 4 times on console, , on. can't figure out problem is. when load html code per ajax request , replace per html() method, same problem. has idea what's wrong?
edit after answer here have following code works:
<script> var currentlink, lastlink; function animatelink(obj) { var el = $(obj); var animate = el.attr('data-animation'); if (animate != undefined) { animate = animate.tolowercase(); animate = animate == 'false' ? false : true; } else { animate = true; } if (animate) { el.toggle('explode'); } } $(document).ready(function () { $("a[data-ajax='true']").on('click', function (event) { event.stopimmediatepropagation(); event.preventdefault(); var gotolink = this.href; animatelink(this); $('#ajax-target').fadeto(0.2, 0.5, function () { lastlink = currentlink; currentlink = gotolink; $('#ajax-target').load(gotolink, {}, function () { $('body').scrolltop(0); $('#ajax-target').fadeto('slow', 1); }); }); }); }); function historyback() { $('#ajax-target').fadeto(0.2, 0.5, function () { var newlink = lastlink; lastlink = this.href; currentlink = newlink; $.get(newlink, {}, function (response) { $('body').scrolltop(0); $('#ajax-target') .html(response) .fadeto('slow', 1); }); }); } </script>
this located on site directly. part not removed when ajax request done. part changed following:
<div class="content-wrapper" id="ajax-target"> @html.partial("layoutcontentheader") <section class="content"> @renderbody() </section> </div>
the loaded page mvc page, main-frame not contain html or body tag, content needs replaced , in addition scripts (how script above):
@if (isajax) { if (ismainlayout) { @html.partial("layoutcontentheader") <section class="content"> @renderbody() </section> } else { @renderbody() } <script> $(function () { $('[data-toggle="tooltip"]').tooltip() }) </script> }
as can see, when in ajax request, send important content.
and code of pressed link example:
<a href="/wilhelmshaven/hse/" data-ajax="true" class="btn btn-block btn-social" style="color: white !important; background-color: rgb(243,156,18)"> <i class="fa fa-th"></i>hse </a>
with no link around (which can force bubbling).
try adding code
$("a[data-ajax='true']").on('click', function (event) { event.stopimmediatepropagation(); var gotolink = this.href; animatelink(this); $('#ajax-target').fadeto(0.2, 0.5, function () { lastlink = currentlink; currentlink = gotolink; $('#ajax-target').load(gotolink, {}, function () { $('body').scrolltop(0); $('#ajax-target').fadeto('slow', 1); }); }); });
reference : http://api.jquery.com/event.stopimmediatepropagation/
Comments
Post a Comment