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.
That works actually really good, but there is one little bug that might cause my browser to crash.
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);
}
}
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.
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.
complete crash of my site!
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.
Any idea what could cause that?
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 url: "sitemap", dataType: "html",
it crashes. I call my sitemap with mydomain.com/sitemap...
the code for the sitemap looks like this:
<?php
/**
* Template Name: Sitemap
*/
?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div id="ajax-base">
<h3>Pages</h3>
<ul>
<?php wp_list_pages('title_li=&depth=0&exclude='); ?>
</ul>
<h3>Posts</h3>
<?php $first = 0;?>
<ul>
<?php
$myposts = get_posts('numberposts=-1&offset=$first');
foreach($myposts as $post) :
?>
<li><a href="<?php the_permalink(); ?>#b"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>
<h3>Categories</h3>
<ul>
<?php wp_list_categories('title_li=&orderby=name'); ?>
</ul>
<h3>Tags</h3>
<ul>
<?php
$tags = get_tags();
foreach ($tags as $tag){
$tag_link = get_tag_link($tag->term_id);
$html .= "<li><a href='{$tag_link}#b' title='{$tag->name} Tag' class='{$tag->slug}'>";
$html .= "{$tag->name}</a></li>";
}
echo $html;
?>
</ul>
</div> <!-- ajax-base -->
<?php endwhile; endif; ?>
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.