Examples of Ajax loading static pages

Source: Internet
Author: User


When the mobile Web page is too long, you can block the content and load it with Ajax.

The main ideas are as follows: Store the pages that need to be loaded in a new page, main code:

header ("Access-control-allow-origin: *");
Header ("content-type:text/html; Charset=utf-8 ");
echo "";
echo inside is the HTML that needs to be loaded.


Then in the JS initialization, use the scroll function to monitor the scroll bar, to determine the distance from the top of the scroll bar, and then use the AJAX get data as needed in the appropriate location, the main code is as follows:

<pre>if (h>appheight) {//scroll bar height is greater than appbox height. Invoke Ajax to get the following data                 / /ajax get method                    if (flag==0) {                     $.get (' http://192.168.1.138/zmitphone/AjaxweChatBox.php ',  function   (response)  {//              

          $ (". Wechatbox"). Remove (". Loading");                   

      $ (". Loadingwechat"). Hide ();                          $ (". Wechatbox"). Append (response)           

              console.log ("Success");                   

      flag=1;                    &NBSP;&NBSP})                  }                 else{//                    

 console.log (flag);                      if (h>appheight+wechatheight) {&NBSP;&Nbsp;                       if (flag==1) {                             $.get (' http://192.168.1.138/zmitphone/ajaxWebBox.php ', function  (response)  {                       

          $ (". Loadingweb"). Hide ();                                  $ (". WebBox"). Append ( Response)                          &nbsP;       console.log ("Success");                   

              flag=2;                    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP})                          }                           else{             

               console.log (flag);                             if (h> Appheight+wechatheight+webheight) {                                  if (flag==2) {                                      $.get (' http://192.168.1.138/zmitphone/ajaxSoftwareBox.php ', function  ( Response)  {                                  

       $ (". Loadingsoft"). Hide ();                                          $ (". Softwarebox"). Append (response)                                         

  console.log ("Success");                                      

    flag=3;                   

                  })                                  }                                   else{                                    

 console.log (flag);                                      if (H>appheight+wechatheight+webheight+softwareheight) {                           &nBsp;             if (flag==3) {                                               $.get (' http://192.168.1.138/zmitphone/ajaxSeoBox.php ', function  (response)  {                                                  $ (". Loadingseo").

Hide ();                                                    $ (". Seobox"). Append (response)                                           

        console.log ("Success");                                      

            flag=4;                                       &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP})                                          }                                            else{                                  

           console.log ("Full load Complete");                                           }                                      }                                   }                               }                          }                      }                  }</pre> <pre>


Because the scroll bar is listening all the time, but at the specified location it is enough to load the data once, and here you can set up a flag, judging by the value of the flag. If you want to add a load hint, you can add to the location to write some hint text, after the successful loading, hide this div can be.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.