Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
PHP Ajax Cookbook

You're reading from   PHP Ajax Cookbook Over 60 simple but incredibly effective recipes to Ajaxify PHP websites with this book and ebook

Arrow left icon
Product type Paperback
Published in Dec 2011
Publisher
ISBN-13 9781849513081
Length 340 pages
Edition Edition
Languages
Arrow right icon
Toc

Table of Contents (16) Chapters Close

PHP Ajax Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
1. AJAX Libraries FREE CHAPTER 2. Basic Utilities 3. Useful Tools Using jQuery 4. Advanced Utilities 5. Debugging and Troubleshooting 6. Optimization 7. Implementing Best Practices to Build Ajax Websites 8. Ajax Mashups 9. iPhone and Ajax Index

Designing simple navigation using jQuery


jQuery is a development framework that allows us to use JavaScript in our HTML document. Now we will build a simple navigation using the basic jQuery features.

Getting ready

Before we can begin, we need to include the latest jQuery library. We can download it from the download section at www.jquery.com. We will save it in our JavaScript folder named js, in the root of our HTML document, for example, cookbook.

All libraries mentioned in this book are also available in an online cache such as http://code.google.com/apis/libraries/.

Note

You can download the example code fles for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the fles e-mailed directly to you.

How to do it...

Now, we can start coding our task1.html page. We'll place it in the cookbook folder.

<!doctype html>
<html>
<head>
  <title>Example 1</title>
</head>

<body>
  <ul id="navigation">
    <li id="home"><a href="#">Home</a></li>
    <li class="active"><a href="#">Our Books</a></li>
    <li><a href="#">Shop</a></li>
    <li><a href="#">Blog</a></li>
  </ul>
  <div id="placeHolder">
    <!-- our content goes here -->
  </div>

  <script src=js/jquery.min.js></"></script>

  <script>
    $(document).ready(function(){
      $('#navigation li a').each(function(){
      var $item = $(this);
        $item.bind('click',function(event){
        event.preventDefault();
          var title = $item.html();
          var html = title + ' was selected.'; 
          $('#placeHolder').html(html);
        });
      });
      $.get('ajax/test.html', function(data) {
        $('.result').html(data);
          alert('Load was performed.');
      });
    });
  </script>
</body>
</html>

How it works...

Now, let's explain what we have done in the preceding code snippet. The main idea of our script is to find each hyperlink <a> in the document, prevent its default functionality, and display the hyperlink content in our placeHolder. From the beginning, we started with doctype and the main HTML layout. The body of the page contains a navigation and a placeholder element for the dynamic content.

The most important part for jQuery functionality is to include our jQuery library. Let's place it before the closing <body> tag. This will allow the HTML of a page to load first:

<script src="js/jquery.min.js"></script>

After loading our HTML page and when the document is ready, we can define our JavaScripts scripts in the $(document).ready() function:

<script>
  $(document).ready(function(){
    alert("Hello jQuery!");
  });
</script>

This can be also shortened to $():

<script>
  $(function(){
    alert("Hello jQuery!");
  });
</script>

The dollar sign $() represents an alias to the jQuery() factory function. Within this function we can use all the CSS selectors like ID, class, or exact tag names. For example:

  • $('a'): Selects all hyperlinks in our document

  • $('#myID'): Selects the element with this ID

  • $('.myID'): Selects all elements with this class

In our case, we are selecting all hyperlinks in the navigation <div> and defining their own functionality with an event handler for click events:

$item.bind('click',function(event){
  // prevent default functionality
  event.preventDefault();
  // here goes the rest
});

And the last step of our example is creating the title VAR and HTML string, which goes to the placeHolder:

var title = $(this).html();
var html = title + ' was selected.';
$('#placeHolder').html(html);

There's more...

The preceding example was really simple. But there is a lot more that jQuery can offer to us. This includes special selectors, effects, DOM manipulation, or AJAX functionality.

We can specify our selectors more precisely. For example, we can specify which hyperlinks should be affected based on their href attributes:

$('a[href^=mailto:]').addClass('mailto);
$('a[href$=.pdf]').addClass('pdf');
$('a[href^=http] [href*=milan]').addClass('milan');

jQuery also covers all possible events (click, blur, focus, dblclick, and so on), visual effects (hide, show, toggle, fadeIn, fadeOut, and so on), or DOM manipulations (appendTo, prependTo, and so on). It has a full suite of AJAX capabilities, which are really easy to use, such as:

$.get('test.html', function(data) {
  $('.result').html(data);
});

But we will have a better look at more jQuery features in further tasks and chapters.

See also

Chapter 1, Ajax using jQuery

Chapter 2, jQuery UI

Chapter 3, Useful tools using jQuery

You have been reading a chapter from
PHP Ajax Cookbook
Published in: Dec 2011
Publisher:
ISBN-13: 9781849513081
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image