jQuery Google Search Newsbar

A jQuery plugin that sets a div or span to show a Google Search Newsbar. If you find this plugin useful please vote for it on the jQuery site.

The current version is 1.0.2 and is available under the GPL and MIT licences. For more detail see the documentation reference page.

Introduction

The Google Search Newsbar functionality can easily be added to a division. It displays a set of news links based on provided search terms. The default search term is 'jquery'.

Google places some restrictions on your use of this service, including obtaining a key specific to a target URL providing the service, making the Web site freely accessible, and showing included Google branding. See the link above for more details.

You can also remove the GSNewsbar widget if it is no longer required.

Default Newsbar:

 

$('#defaultNewsbar').gsnewsbar();

$('#removeNewsbar').toggle(
	function() {
		$(this).text('Re-attach');
		$('#defaultNewsbar').gsnewsbar('destroy');
	},
	function() {
		$(this).text('Remove');
		$('#defaultNewsbar').gsnewsbar();
	}
);

You can override the defaults globally as shown below:

$.gsnewsbar.setDefaults({horizontal: true});

Processed fields are marked with a class of hasGSNewsbar and are not re-processed if targetted a second time.

Formatting Options

You have some control over the appearance of the newsbar. The settings marked with an asterisk (*) only apply to the vertical layout.

Formatting:

 

 

 

$('#formatNewsbar').gsnewsbar();
		
var targets = [$.gsnewsbar.targetSelf, $.gsnewsbar.targetBlank,
	$.gsnewsbar.targetTop, $.gsnewsbar.targetParent];
		
$('#format input,#format select').change(function() {
	$('#formatNewsbar').gsnewsbar('change',
		{title: $('#title').val(),
		horizontal: $('#orientation').val() == 'true',
		verticalCompressed: $('#compressed').val() == 'true',
		manyResults: $('#results').val() == 'true',
		linkTarget: targets[$('#target').val()]});
});

Miscellaneous

You can show more detail about a news entry when in horizontal mode by providing the jQuery selector for a container.

Extra info:

 

$('#extraNewsbar').gsnewsbar({currentResult: '#extraInfo'});

Cycling Searches

You can set the newsbar up to cycle automatically through a set of search terms, with control over the interval between cycles and the order. Just pass an array of search terms when attaching the newsbar.

For horizontal newsbars, the cycle time is the interval between showing individual news entries, whereas for vertical ones it is the interval between showing news entries for one of the search terms.

For both orientations, the mode dictates the order of the search terms, not the individual news entries.

Cycling:

 

 

$('#cycleNewsbar').gsnewsbar(
	{search: ['jquery', 'australia', 'cats', 'tennis']});
	
var times = [$.gsnewsbar.cycleVShort,
	$.gsnewsbar.cycleShort, $.gsnewsbar.cycleMedium,
	$.gsnewsbar.cycleLong, $.gsnewsbar.cycleManual];
var modes = [$.gsnewsbar.cycleRandom, $.gsnewsbar.cycleLinear];
		
$('#cycle select').change(function() {
	$('#cycleNewsbar').gsnewsbar('change',
		{cycleTime: times[$('#cycleTime').val()],
		cycleMode: modes[$('#cycleMode').val()],
		horizontal: $('#orientation2').val() == 'true'});
});

In the Wild

This tab highlights examples of this plugin in use "in the wild".

To add another example, please contact me (kbwood{at}iinet.com.au) and provide the plugin name, the URL of your site, its title, and a short description of its purpose and where/how the plugin is used.

Quick Reference

A full list of all possible settings is shown below. Note that not all would apply in all cases. For more detail see the documentation reference page.

$(selector).gsnewsbar({
	horizontal: true, // True for horizontal display, false for vertical
	verticalCompressed: false, // True for compressed layout when vertical,
		// false for expanded
	title: '', // Title for the bar
	search: 'jquery', // Single or list of search terms
	manyResults: false, // True for many results, false for only a few
	cycleTime: this.cycleManual, // Time between cycles of the search terms
	cycleMode: this.cycleLinear, // Mode of cycling through the search terms
	linkTarget: this.targetSelf, // Control where the news links open
	currentResult: '' // jQuery selector, jQuery object, or element for 
		// additional info for current entry when horizontal
});

Usage

  1. Include the jQuery library in the head section of your page.
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
  2. Set your Google Search key for use with your Web site.
    <script type="text/javascript">
    $.googleSearchKey = 'ABQIAAAA...2Z_iWfuw';
    </script>
  3. Download and include the jQuery GSNewsbar CSS and JavaScript in the head section of your page.
    <style type="text/css">@import "jquery.gsnewsbar.css";</style>
    <script type="text/javascript" src="jquery.gsnewsbar.js"></script>
    Alternately, you can use the packed version jquery.gsnewsbar.pack.js (3.0K vs 6.8K) or minified version jquery.gsnewsbar.min.js (3.2K, 1.3K when zipped).
  4. Connect the GSNewsbar functionality to your divs or spans.
    $(selector).gsnewsbar({search: ...});

For more detail see the documentation reference page.

Comments

None as yet.

Contact Keith Wood at kbwood{at}iinet.com.au with comments or suggestions.

Change History

VersionDateChanges
1.0.228 May 2011
  • Updated for jQuery 1.6
1.0.126 Dec 2009
  • Corrected initialisation in Chrome
1.0.029 Nov 2008
  • Initial release

Valid HTML 4.01 Strict