简体   繁体   中英

Use JQuery Ajax to load static html page to a div tag

I got this working but I need a default page showing before you choose one, how do I do this? Also is it possible to hide the page files? So it will not show when you View Source.

    <script src="http://code.jquery.com/jquery-1.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
        $(document).ready(function(){
           $("#page1").click(function(){
            $('#result').load('latestmembers.php');
             //alert("Thanks for visiting!");
           }); 

           $("#page2").click(function(){
            $('#result').load('Females.php');
             //alert("Thanks for visiting!");
           });
         });
    </script>

<ul>
    <li><a id="page1" href="#">Latest Members</a></li>
    <li><a id="page2" href="#">female members</a></li>
 </ul>
<div id="result" style="clear:both;">
</div>

Default will load on load and then you can choose to see just latest members or female members.

$(document).ready(function(){
      $('#result').load('latestmembers.php'); // default page
       $("#page1").click(function(){
        $('#result').load('latestmembers.php');
         //alert("Thanks for visiting!");
       }); 

       $("#page2").click(function(){
        $('#result').load('Females.php');
         //alert("Thanks for visiting!");
       });
     });

JavaScript is client side code, you cannot hide anything from source code.

Fitst Question : Default Page

  $(document).ready(function(){
       $("#page1").click(function(){
        $('#result').load('latestmembers.php');  
         //alert("Thanks for visiting!");
       }); 

       $("#page2").click(function(){
        $('#result').load('Females.php');
         //alert("Thanks for visiting!");
       });

    // Default page load 
      $('#result').load('defaultPage.php');

     });

Second Question: hide page name

Basically whatever you write on JavaScript is view able by user So you can opt any work around (Dont think its a good idea) You can call a single page like getPage.php with arguments and desire what content to return by different argument Example Below

   $(document).ready(function(){
       $("#page1").click(function(){
        $('#result').load('getPage.php?pid=lm');  // by the pid(page ID) you can check that its for latestmembers.php
         //alert("Thanks for visiting!");
       }); 

       $("#page2").click(function(){
        $('#result').load('getPage.php?pid=fe');  // by the pid(page ID) you can check that its for Females.php
         //alert("Thanks for visiting!");
       });

    // Default page load  with no argument
      $('#result').load('getPage.php');

     });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM