I am using jquery to run this search, so initally I made a link to a jquery file.
Within the header I added the following jquery scripts. It searches for a numeric value, if that value is part of the xml document then the user is redirected to the desired page. If it is a numeric value that does not match the xml document, an error is returned. If the search term is not a number then the term is searched in the google search.
<script type="text/javascript">
$(document).ready(function() {
var searchVal;
var found;
var options = {
beforeSubmit: function(formData, jqForm, options) {
var form = jqForm[0];
searchVal = form.query.value + ".html";
},
success: function(responseText, statusText) {
$(responseText).find("url").each(function() {
var url = $(this).text();
if (url.match(searchVal + "$") == searchVal) {
window.location = url;
found = true;
}
});
if (!found) {
alert("Page not found. Please enter a new search term.");
}
}
};
$("#searchForm").submit(function() {
$(this).ajaxSubmit(options);
return false;
});
});
</script>
Here is the code for the form.
<form id="searchForm" action="search.xml">
Enter search term:
<input type="text" name="query" />
<input type="submit" />
</form>
No comments below.
Add your thoughts