Jump to content

Jquery .load()


dotkpay

Recommended Posts

I need to put the function into onclick. The page to be loaded into the div should not load on start-up but should be called when a button is clicked.

This is the script:

 

<!DOCTYPE html>

<html>

<head>

  <style>

  body{ font-size: 12px; font-family: Arial; }

  </style>

  <script src="http://code.jquery.com/jquery-latest.js"></script>

</head>

<body>

 

<b>Successful Response (should be blank):</b>

<div id="success"></div>

<b>Error Response:</b>

<div id="error"></div>

 

<script>

$("#success").load("/not-here.php", function(response, status, xhr) {

  if (status == "error") {

    var msg = "Sorry but there was an error: ";

    $("#error").html(msg + xhr.status + " " + xhr.statusText);

  }

});

  </script>

 

</body>

</html>

Link to comment
https://forums.phpfreaks.com/topic/242524-jquery-load/#findComment-1245576
Share on other sites

You'll want to look at jQuery events too then. In particular the click event.

 

Also, be aware that jQuery code generally needs to be loaded after the DOM is finnished loading. It has a special event you attach to the document to do just that.

 

So, a complete example might look like:

 

<!DOCTYPE html>
  <html>
    <head>
      <script src="http://code.jquery.com/jquery-latest.js"></script>
    </head>
    <body>
      <span id="foo">click here</span>
      <div id="bar"></div>
    </body>
    <script>
      $(document).ready(function() {
        $('#foo').click(function() {
          $('#bar').load("/somefile.php");
        });
      });
    </script>
</html>

Link to comment
https://forums.phpfreaks.com/topic/242524-jquery-load/#findComment-1245586
Share on other sites

Thanks very much Thorpe,

 

But how do I make the script load varying urls instead of just one without rewriting the functions. For example if I have multiple onclick events and I want to use the same exact functions:

 

<!DOCTYPE html>

  <html>

    <head>

      <script src="http://code.jquery.com/jquery-latest.js"></script>

    </head>

    <body>

      <span id="foo">click here</span>

      <div id="bar"></div>

    </body>

    <script>

      $(document).ready(function() {

        $('#foo').click(function() {

          $('#bar').load("/somefile.php");

        });

      });

    </script>

</html>

Link to comment
https://forums.phpfreaks.com/topic/242524-jquery-load/#findComment-1245614
Share on other sites

You really should find some jQuery tutorials. It's very easy to use once you get the hang of it.

 

One easy thing you could do would be to embed the name of the page you want to include within a rel tag, then grap it from there.

 

<!DOCTYPE html>
  <html>
    <head>
      <script src="http://code.jquery.com/jquery-latest.js"></script>
    </head>
    <body>
      <a href=#" class="link" rel="about">about</a>
      <a href=#" class="link" rel="whatever">whatever</a>
      <a href=#" class="link" rel="whateverelse">whatever else</a>
      <div id="bar"></div>
    </body>
    <script>
      $(document).ready(function() {
        $('.link').click(function() {
          $('#bar').load('/' + $(this).attr('rel') + '.php');
        });
      });
    </script>
</html>

Link to comment
https://forums.phpfreaks.com/topic/242524-jquery-load/#findComment-1245625
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.