jQuery script is looping for unknown reason
<p>I have problem with jQuery script which is looping random number of times and I have no idea why. Here is my code: </p>

<pre><code>    &lt;!DOCTYPE html&gt;

    &lt;html lang="en"&gt;

    &lt;head&gt;
    &lt;?php
    include 'head.php'
    ?&gt;
    &lt;/head&gt;

    &lt;body&gt;
    &lt;div id="container"&gt;

    &lt;!-- Header --&gt;

    &lt;?php
    include 'header.php'
    ?&gt;

    &lt;!-- Navigation Bar --&gt;

    &lt;?php
    include 'navigation.php'
    ?&gt;


  &lt;!-- Gallery section --&gt;

  &lt;section class="articles"&gt;

    &lt;h1&gt;&lt;a href="projects.php?cat=All"&gt;Projects&lt;/a&gt;&lt;/h1&gt;
    &lt;hr&gt;

    &lt;div id="infoblock"&gt;

    &lt;?php


    if (!empty($_GET['id'])) {
    require 'info.php';
    }

    else {
      if (!empty($_GET['cat'])) {
        require 'maingallery.php';
      }

      else {
        echo '&lt;p&gt;Database error, please reload your page';
      };
    };

    ?&gt;

    &lt;/div&gt;

  &lt;/section&gt;

  &lt;!-- Footer --&gt;

  &lt;?php
  include 'footer.php'
  ?&gt;


&lt;/div&gt;
   &lt;/body&gt;

   &lt;/html&gt;
</code></pre>

<p>So my problem is with required maingallery.php .This is code for maingallery.php: </p>

<pre><code>    &lt;div id="filterbuttons"&gt;
    &lt;h4 class="selected" id="All" href="gallery.php" &gt;All&lt;/h4&gt;
    &lt;h4 id="Residential" href="gallery.php?cat=Residential" &gt;Residential&lt;/h4&gt;
    &lt;h4 id="Modernisation+%26+Domestic+Extensions" href="gallery.php?cat=Modernisation+%26+Domestic+Extensions" &gt;Modernisation &amp; Domestic Extensions&lt;/h4&gt;
    &lt;h4 id="Feasibility+Layouts" href="gallery.php?cat=Feasibility+Layouts" &gt;Feasibility Layouts&lt;/h4&gt;
    &lt;h4 id="Master+Planning" href="gallery.php?cat=Master+Planning" &gt;Master Planning&lt;/h4&gt;
    &lt;/div&gt;


    &lt;ul class="gallery"&gt;

    &lt;div id="gallerylist"&gt;  
       &lt;?php require 'gallery.php'; ?&gt;
    &lt;/div&gt;

    &lt;/ul&gt;
</code></pre>

<p>and this is gallery.php : </p>

<pre><code>    &lt;?php

    if (!empty($_GET)) {
        $cat = urldecode($_GET['cat']);
    }
    else {
        $cat = 'All';
    };

    $dbc = mysqli_connect('server.microlite10.com','XXXXXX','XXXXXX','avd');

    if ($dbc) {
        getGallery($cat, $dbc);
    }
    else {
        echo '&lt;p&gt;Database error, please refresh your screen!&lt;/p&gt;';
    };


    function getGallery($cat, $dbc) {
        $r = 'SELECT * FROM Projects';
        $q = mysqli_query($dbc,$r);
        while ( $row = mysqli_fetch_array( $q, MYSQLI_ASSOC) ) {

            if ( $cat == 'All' ) {
                echo '&lt;li class="item"&gt;
                &lt;a class="info" href="info.php?id='.$row['id'].'"&gt;&lt;img src="'.$row['thumbnail'].'" width="212" height="119" alt="'.$row['name'].'"&gt;&lt;/a&gt;
                &lt;h2&gt;&lt;a class="info" href="info.php?id='.$row['id'].'"&gt;'.$row['name'].'&lt;/a&gt;&lt;/h2&gt;
                &lt;h3&gt;&lt;a class="cat" href="gallery.php?cat='.urlencode($row['category']).'"&gt;'.$row['category'].'&lt;/a&gt;&lt;/h3&gt;&lt;/li&gt;';   
            }

            else {
                if ( $row['category']==$cat) {

                    echo '&lt;li class="item"&gt;
                    &lt;a class="info" href="info.php?id='.$row['id'].'"&gt;&lt;img src="'.$row['thumbnail'].'" width="212" height="119" alt="High Tor East, Earl Shilton"&gt;&lt;/a&gt;
                    &lt;h2&gt;&lt;a class="info" href="info.php?id='.$row['id'].'"&gt;'.$row['name'].'&lt;/a&gt;&lt;/h2&gt;
                    &lt;h3&gt;&lt;a class="cat" href="gallery.php?cat='.urlencode($row['category']).'"&gt;'.$row['category'].'&lt;/a&gt;&lt;/h3&gt;&lt;/li&gt;';

                };    
            };
        };
    }




    ?&gt;
    &lt;script type="text/javascript"&gt;



        // Assign $cat value to variable
        // and find relative &lt;h4&gt; element to assign selected class
        var cat = "&lt;?php echo urlencode($cat); ?&gt;";
        alert('script executed');
        $("h4").each(function () {
          var id = $(this).attr('id');
          if (cat == id) {
            $(this).addClass('selected');
          }
          else {
            $(this).removeClass('selected');
          };
        });

        $("li.item:visible").each(function(i) {
            if((i+1)%4==0) $(this).css("margin","30px 0 30px 0px");
            else $(this).css("margin","30px 20px 30px 0");
        });

        $('.cat').bind('click', function(e) {
            var href = $(this).attr('href');
            $('#gallerylist').load(href);
            e.preventDefault();
        });

        $('h4').bind('click', function(e) {
            var href = $(this).attr('href');
            $('#gallerylist').one().load(href);
            var stateObj = { foo : "bar" };
            var newurl = $(this).attr('id');
            history.pushState(stateObj, "", ('http://www.damianwojtczak.com/avd2/projects.php?cat='+newurl));
            e.preventDefault();
        });

        $('.info').bind('click' , function(e) {
            var href = $(this).attr('href');
            $('#infoblock').load(href);
            e.preventDefault();
        });


  &lt;/script&gt;
</code></pre>

<p>so my problem is with jQuery script added to the gallery.php file. When I click on any of the h4 elements, jQuery is reloading #gallerylist element with new content and is preventing default action. For the first few clicks it works fine, but than it seems like the script from 'gallery.php' file is looping for some reason without clicking on any h4 element. I have added 'alert' message to check how many times my script is executed and can't figure out why is it looping. </p>

<p>You can check this issue here <a href="http://www.damianwojtczak.com/avd2/projects.php?cat=All" rel="nofollow">http://www.damianwojtczak.com/avd2/projects.php?cat=All</a> , just try to click on filtering elements (All, Residential etc.) and you will notice that script is looping as alert appears few times. </p>
