Check page load

There is a set of radio lenses on my page. When you select any of them, I will forcefully return to the server.

By default, one of the switches when the page is initially loaded. But then I do not want this button to launch my jQuery script by default when loading the first page, because this causes an unnecessary postback. As soon as the page loads, it is normal if later this switch is selected, that it sends messages back, and not on page loading.

Is there any way to check page loading in jQuery? I looked around, but it had not yet surfaced in my search.

+3
source share
3 answers

jQuery uses ready for this.

$(document).ready(functionToCallHere);

:

$(function() {
    // Anything here will be called on page load.
});

, DOM , , .

JavaScript load/onload ( , ).

: jQuery $(window).load(functionToCallHere), , .

+2

<head> -tag :

<script type="text/javascript">
  var loading = true;

</script>

loading .

false, :

$(function() {
  // My init code
  $("select the radio")
    .val("my init value");
    .change(function() {
     if (loading) return;
     $.post( 
         //... 
     );
  });
  loading = false;
});
+1

How about using $ (document) .ready ()? Does it fit your needs?

0
source

Source: https://habr.com/ru/post/1749046/


All Articles