title: jquery: ajax based search - browser crash?
body: <p>hey guys,
  i'm working on a rather weird ajax based search model. The search is not actually retrieving real searchresults from the server but rather loads the website's sitemap (with the jquery load() method) and extracts its links.</p>
  
  <p>That works actually really good, but there is one little bug that might cause my browser to crash.</p>
  
  <pre><code>var searchTimer = 0;
  
  $('.s').keyup(function(e) {
  
      switch (e.keyCode) {
          //case 8:  // Backspace
          case 9:  // Tab
          case 13: // Enter
              doSearch(e.keyCode);
              break;
          case 16: // Shift
          ...
          case 37: // Left
              break;
          case 38: // Up
              doSearch(e.keyCode);
              break;
          case 39: // Right
              break;
          case 40: // Down
              doSearch(e.keyCode);
              break;
          ...
          break;
  
          default:
          if (searchTimer != 0) {
              clearTimeout(searchTimer);
          }
  
          searchTimer = setTimeout(function () {
              doSearch(e.keyCode);
          }, 250);
      }
  
  });
  
  function doSearch(keyCode) {
  
      if ($('.s').val() != '') {
  
          searchTimer = 0;
  
          $sr.load('/sitemap/', function() {
  
  
          }); // end load
  
      } else {
          clearsearch(true);
      }
  }
  </code></pre>
  
  <p>The only problem I have with this is that the site crashes once I type a word in my .s input field and immediately delete it within the 250ms.</p>
  
  <p>Imagine this:
  1.) the input is empty.
  2.) i quickly type "test".
  3.) the doSearch function hasn't even been fired and I hit cmd-a to select all and remove the text in the input.</p>
  
  <p>complete crash of my site!</p>
  
  <p>Why could that happen? It does work really smooth and fine when I just type "test" or delete the input once the doSearch() has been fired. It actually works always. Just in this rare case of typing quickly and removing the typed text within the event of doSeach() it crashes.</p>
  
  <p>Any idea what could cause that?</p>
  
  <p>edit/update:
  When I copy sitemap.html into my current procets root directory and load it doesn't crash and works fine as in your example. As soon as i change it to <code>url: "sitemap", dataType: "html",</code> it crashes. I call my sitemap with mydomain.com/sitemap...</p>
  
  <p>the code for the sitemap looks like this:</p>
  
  <pre><code>    &lt;?php
  /**
   * Template Name: Sitemap
   */
  ?&gt;
  &lt;?php if (have_posts()) : while (have_posts()) : the_post(); ?&gt;
  
      &lt;div id="ajax-base"&gt;
          &lt;h3&gt;Pages&lt;/h3&gt;
              &lt;ul&gt;
                  &lt;?php wp_list_pages('title_li=&amp;depth=0&amp;exclude='); ?&gt;
              &lt;/ul&gt;
          &lt;h3&gt;Posts&lt;/h3&gt;
              &lt;?php $first = 0;?&gt;
              &lt;ul&gt;
              &lt;?php
              $myposts = get_posts('numberposts=-1&amp;offset=$first');
              foreach($myposts as $post) :
              ?&gt;
              &lt;li&gt;&lt;a href="&lt;?php the_permalink(); ?&gt;#b"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/li&gt;
              &lt;?php endforeach; ?&gt;
              &lt;/ul&gt;
          &lt;h3&gt;Categories&lt;/h3&gt;
              &lt;ul&gt;
                  &lt;?php wp_list_categories('title_li=&amp;orderby=name'); ?&gt;
              &lt;/ul&gt;
          &lt;h3&gt;Tags&lt;/h3&gt;
              &lt;ul&gt;    
                  &lt;?php
                  $tags = get_tags();
                  foreach ($tags as $tag){
                      $tag_link = get_tag_link($tag-&gt;term_id);
                      $html .= "&lt;li&gt;&lt;a href='{$tag_link}#b' title='{$tag-&gt;name} Tag' class='{$tag-&gt;slug}'&gt;";
                      $html .= "{$tag-&gt;name}&lt;/a&gt;&lt;/li&gt;";
                  }
                  echo $html;
                  ?&gt;
              &lt;/ul&gt;
      &lt;/div&gt; &lt;!-- ajax-base --&gt;
  
  &lt;?php endwhile; endif; ?&gt;
  </code></pre>
  
  <p>sorry for this last question, but any idea why that makes a difference. When I use this dynamic /sitemap as basis for my search the browser crashes. With a static html page it works fine.</p>