javascript - attach eventListener only on document.ready -
is there way attach event document.ready? example take @ code
$(document).ready(function(){ $('body').load('eg_pageload.html'); $(document).ajaxstart(function(){ console.log('say console'); }); $('#trigger').on('click', function(){ $('body').load('file.txt'); }); });
so, when user click on #trigger
ajaxstart did not activated. in other words ajaxstart should triggered when ajax request start while page loading, otherwise don't start ajaxrequest. tried unbind ajaxrequest on ajaxcomplete function, doesn't work because somepage @ site don't call ajax request @ page loading, instead it's call ajax request on click event example.
if understand right wanting disable ajaxstart
trigger on specific ajax
calls. typically done so:
$.ajax({ url: "test.html", global: false, ... });
note global: false,
line. used for.
from ajaxstart docs
additional notes:
if $.ajax() or $.ajaxsetup() called global option set false, .ajaxstart() method not fire.
from ajax docs
global (default: true) type: boolean whether trigger global ajax event handlers request. default true. set false prevent global handlers ajaxstart or ajaxstop being triggered. can used control various ajax events.
you using .load()
method option not available. not available similar .get()
method, both short-hand ajax functions can use ajax
so.
$(document).ready(function() $(document).ajaxstart(function(){ console.log('say console'); }); $('#trigger').on('click', function(){ $.ajax({ url: 'file.txt', global: false, datatype: 'html' }); .done(function( html ) { $( 'body' ).append( html ); }); }); });
this going way avoid ajaxstart
on request , not matter in dom
load takes place.
the above jquery
example, may minor mistakes, links provided cover process in full.
Comments
Post a Comment