137 lines
3.9 KiB
Plaintext
137 lines
3.9 KiB
Plaintext
http://forrst.com/posts/jquery_livesearch_for_twentyten_theme-tV3
|
|
|
|
//livesearch.js
|
|
|
|
jQuery(document).ready(function ($) {
|
|
var contentCache, searched = false;
|
|
jQuery('#searchform').submit(function () {
|
|
var s = jQuery(this).find('#s').val();
|
|
if (s.length == 0) {
|
|
return;
|
|
}
|
|
var submit = $('#searchsubmit');
|
|
submit.attr('disabled', false);
|
|
var url = jQuery(this).attr('action') + '?s=' + encodeURIComponent(s) + '&action=search_ajax'
|
|
jQuery.ajax({
|
|
url: url,
|
|
type: 'get',
|
|
dataType: 'html',
|
|
beforeSend: function () {
|
|
|
|
submit.attr('disabled', true).fadeTo('slow', 0.5);
|
|
document.body.style.cursor = 'wait';
|
|
var load = '<div id="content" role="main"><h1 class="page-title">Searching...</h1></div>';
|
|
jQuery('#container').empty().html(load);
|
|
},
|
|
success: function (data) {
|
|
submit.attr('disabled', false).fadeTo('slow', 1);
|
|
document.body.style.cursor = 'auto';
|
|
jQuery('#container').empty().html(data);
|
|
jQuery('#ajaxback').click(function () {
|
|
jQuery('#container').empty().html(contentCache);
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
return false;
|
|
});
|
|
var timer, currentKey;
|
|
jQuery('#s').keyup(function () {
|
|
clearTimeout(timer);
|
|
timer = setTimeout(function () {
|
|
var sInput = jQuery('#s');
|
|
var s = sInput.val();
|
|
if (s.length == 0) {
|
|
if (searched) {
|
|
jQuery('#container').empty().html(contentCache);
|
|
sInput.focus();
|
|
//jQuery('#search-form span.processing').remove();
|
|
searched = false;
|
|
}
|
|
currentKey = s;
|
|
} else {
|
|
if (s != currentKey) {
|
|
if (!searched) {
|
|
contentCache = jQuery('#container')[0].innerHTML;
|
|
searched = true;
|
|
}
|
|
currentKey = s;
|
|
if (s != ' ') {
|
|
jQuery('#searchform').submit();
|
|
jQuery('#s').attr('value','');
|
|
|
|
}
|
|
}
|
|
}
|
|
}, 800);
|
|
});
|
|
|
|
});
|
|
|
|
//functions.php
|
|
|
|
add_action('init','ajax_live_search_js');
|
|
|
|
function ajax_live_search_js() {
|
|
wp_enqueue_script('jquery');
|
|
wp_register_script('ajax_live_search', get_bloginfo('stylesheet_directory') . '/js/2010.js', array('jquery'), '1.0');
|
|
wp_enqueue_script('ajax_live_search');
|
|
}
|
|
|
|
function ajax_live_search_template() {
|
|
if (is_search()) {
|
|
include(dirname(__FILE__) . '/livesearch.php');
|
|
exit;
|
|
}
|
|
}
|
|
|
|
add_action('template_redirect','ajax_live_search_template');
|
|
|
|
//livesearch.php
|
|
|
|
<?php
|
|
|
|
if(!$_GET['action']) {
|
|
get_header();
|
|
echo '<div id="container">';
|
|
}
|
|
|
|
else { $do_ajax =1;}
|
|
|
|
?>
|
|
|
|
<div id="content" role="main">
|
|
|
|
<?php if ( have_posts() ) : ?>
|
|
|
|
<h1 class="page-title"><?php printf( __( 'Search Results for: %s', 'twentyten' ), '<span>' . get_search_query() . '</span>'); ?></h1>
|
|
|
|
<?php get_template_part( 'loop', 'search' );?>
|
|
|
|
<?php else : ?>
|
|
|
|
<div id="post-0" class="post no-results not-found">
|
|
|
|
<h2 class="entry-title"><?php _e( 'Nothing Found', 'twentyten' ); ?></h2>
|
|
|
|
<div class="entry-content">
|
|
|
|
<p><?php _e( 'Sorry, but nothing matched your search criteria. Please try again with some different keywords.', 'twentyten' ); ?><?php if($do_ajax ==1){ echo'<a id ="ajaxback" href="javascript:void(0);" title="Back to last page">Go back to last page?</a>';} ?></p>
|
|
|
|
</div><!-- .entry-content -->
|
|
</div><!-- #post-0 -->
|
|
|
|
<?php endif; ?>
|
|
|
|
</div><!-- #content -->
|
|
|
|
<?php
|
|
if(!$_GET['action']) {
|
|
echo '</div><!-- #container -->';
|
|
get_sidebar();
|
|
get_footer();
|
|
}
|
|
|
|
?>
|