Chris Norton

Log in

I have added in a modified version of Alex King’s Friendly Search URLs. What this basically does is add a rewrite rule and some JavaScript that changes http://www.chnorton.com.au/?s=friendly%20search into http://www.chnorton.com.au/search/friendly+search, which I think everyone can agree is much nicer (but both will work). It’s also easy enough to remember that you can type it into the address bar of a browser directly.

I have been wanting to try out some JavaScript programming for a while and I’m especially keen to get into jQuery so I decided to rewrite the JavaScript code to use that. Using jQuery is actually quite cool; doing operations on the DOM is simplicity itself. I transformed the original code into the following:

(Line wrap marked with »)

$(document).ready(function() {
	$("#searchform").submit(function() {
		location.href='http://www.chnorton.com.au/search/' + »
			$("#searchbox").val().replace(/\ /g, '+'); 
		return false;
	});
});

Even though it’s a little longer, it attaches to the onsubmit handler via DOM manipulation, rather than messing up the HTML, which makes it a bit easier to use and keeps a cleaner separation between the markup and the JavaScript. Good coding practice and all that (although apparently it also executes slightly slower).

As an introduction to jQuery I can say that I’m impressed by how quickly I could create some DOM manipulation code, especially considering that I haven’t really done much work with JavaScript before. I have some things in mind for further modifications to the site so look out for those in the near future.

Of course, the only problem with all this is that it relies on JavaScript to work so I have been looking into some alternatives that run on the server side. Hopefully I’ll be adding one of these in soon too.

Comments are closed.