I’ve had a snippet of jQuery and javascript kicking around for a while, that I have found handy in the past for generating post slugs. Finally I’ve packaged it as a plugin to share: jQuery Slug Plugin.
This is my first jQuery plugin, so it doesn’t follow the more advanced plugin development pattern by Mike Alsup, instead I followed a more rudimentary plugin authoring tutorial.
Here’s an explanation of the plugin, and examples of its usage.
That’s a nice little plugin you’re putting together.
One way you might be able to avoid the hyphens piling up when spaces are repeated is to modify the regex slightly:
var slugcontent_hyphens = slugcontent.replace(/\s+/g,'-');Keep up the good work!
Thanks for the improved regex, Karl! I’ll add that and a few other improvements to the next release.
I take your jquery plugin and combined it with urlify function from Django (BSD Licence) framework generated admin. You can take it and improve. Here it is:
http://dvanula.cz/jquery.slug2.js
Comments for your todo:
Ajax should stay away from this plugin, I think. For that you can use:
http://malsup.com/jquery/form/
Thanks for this plugin it saved me a lot of time messing with regex and such. I know this is almost a year old now but I would like to contribute one suggestion. I added the “trim()” function to the slug content before adding the hyphens this prevents extra hyphens if spaces are left at the beginning or end of the title.
var slugcontent = $this.val();
var slugcontent_trim = jQuery.trim(slugcontent);
var slugcontent_hyphens = slugcontent_trim.replace(/\s+/g,’-');
http://docs.jquery.com/Utilities/jQuery.trim
Brooke