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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.