W3Schools Learner's Blog

W3Schools Programming knowledge summary website

div

2/07/2020

four common jquery initialization function example

Jquery initialization function is almost used to any website, we sometimes do forget how to use the jquery init method,  so I want to write an article as my notes.

How does the jquery initialize? First we need to import the jquery  package, as follows:

  1.  <script type="text/javascript" src="./js/jquery.min.js"></script>

We have four common jquery init method

Jquery init method one:

  1. <script type="text/javascript">

  2. $(document).ready(function(){
  3.  
  4.   console.log("enter jquery init method one...");
  5.  
  6. });

  7. </script>

Jquery init method two:

  1. <script type="text/javascript">
  2.  
  3. $(function(){
  4.  
  5.    console.log("enter jquery init method two...");
  6.  
  7. });
  8.  
  9. </script>

Jquery init method three:

  1. <script type="text/javascript">
  2.  
  3. jQuery(function($){
  4.  
  5.   console.log("enter jquery init method three...");
  6.  
  7. });
  8.  
  9. </script>

Jquery init method four:

  1. <script type="text/javascript">
  2.  
  3. (function ($) {

  4.   console.log("enter jquery init method four...");

  5. })(jQuery);

  6. </script>

note:

If we use original JavaScript, we want to use the init method, we don't need to import the jquery.min.js package. We can use the onload method inside <body> tag, as follows:

  1. <script type="text/javascript">

  2. window.onload=function(){

  3. //write something
  4. }

  5. </script>
Thank you!

No comments:

Post a Comment

Note: only a member of this blog may post a comment.