I want use ajax on ASP.NET platform. For that I use ScriptManager. Simply I add this script with jQuery after when "document is ready".
$(document).ready(function () {
// sync
{{scopeName}}_init();
// async
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(function (sender, args) {
{{scopeName}}_init();
});
});
And then such a mysterious javascript error happened.
Uncaught ReferenceError: Sys is not defined
Q what I have wrong if javascript stop working after first request?
Solution is not skip "Sys.WebForms.PageRequestManager" using undefined condition
if (typeof(Sys) !== 'undefined') {
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(function (sender, args) {
{{scopeName}}_init();
});
}
For the postback we init function just if document is ready - sync section.
For ajax request in ScriptManager block we need to register init function on "add_pageLoaded". After that everytime script works fine at first request - async section.
Most important neglected step is register also "add_endRequest", and that fix the problem.
Whole example code:
$(document).ready(function () {
// sync
console.log("sync");
{{scopeName}}_init();
// async
pageRequestManager = Sys.WebForms.PageRequestManager.getInstance();
pageRequestManager.add_endRequest({{scopeName}}_onAsyncEndRequest);
pageRequestManager.add_pageLoaded({{scopeName}}_onAsyncPageLoaded);
});
function {{scopeName}}_onAsyncEndRequest(sender, args) {
console.log("async end");
console.log(args);
}
function {{scopeName}}_onAsyncPageLoaded(sender, args) {
console.log("async start");
{{scopeName}}_init();
}