JavaScript string replace for post slug

August 22, 2005 / Filed under: JavaScript, Tutorials

My own administration panel, for this blog, helps me insert new entries into my database.

On my "Add New Entry" form, I have many fields. Two important fields are headline and post slug. The headline is, obviously, the headline of the entry. The post slug is the name that appears in the permanent URL for the entry.

For example, if you look up to the address bar, in the browser, you’ll see the permanent URL, for this entry. You’ll notice a handful of useful information, such as the year, month, and day - but at the very end is the post slug. It’s basically the headline "crunched down."

In other words, the headline for this entry is JavaScript string replace for post slug. So, the post slug is therefore: javascript-string-replace-for-post-slug. Notice how the entire phrase is lower case. Also, all spaces have been removed, and replaced with a dash (-).

When I go to post a new entry, I don’t like to have to type things twice. So, after I enter the headline, I have some simple JavaScript automatically enter the post slug.

For example, try entering a headline in the text box below, and then click out of the box, and watch how the text is automatically copied over to the post slug field:

Headline
Post slug

The HTML

In the headline field, in the HTML form, we have something similar to this:

<input type="text" name="headline" onBlur="headline_to_postslug('2005-08-21a', 'headline', 'post_slug')" />

Notice the onBlur attribute calls a function named headline_to_postslug, which is a JavaScript function that takes three parameters.

The JavaScript

It’s not perfect

Of course, it’s not perfect. The post slug can only contain letters, numbers, or dashes - but the headline might contain characters like double quotes ("), single quotes (‘ ’), parenthesis, commas (,), and much more.

Currently, the JavaScript function I created does not check for all those other characters. It simply checks for spaces.

And that’s OK. The majority of my headlines won’t contain all of those other characters - and I don’t mind if I have to edit the post slug field, in the case that those characters do exist, in the headline.

And, obviously, if we wanted, we could easily add some more JavaScript to make those other characters automatically disappear, too.

But, for now, we’ll just keep it simple.

Comments/Mentions

# dale at 8/22/2005 11:10 pm cst

I do that based on a php function so that clients don't need to have javascript going.

Also it is a good idea to check input because the javascript stuff can be changed.

/off topic:

Have you been getting any spam through your contact form?

Also you have "" at the very top left hand side of your website, I've had that issue before, something to do with Dreamweaver I think.

# Matthom at 8/23/2005 5:48 am cst


Have you been getting any spam through your contact form?

Ever since I added the security check, spam has dwindled considerably. However, I still think it's capable of getting through.


Also you have "" at the very top left hand side of your website, I've had that issue before, something to do with Dreamweaver I think.

Well... I don't use Dreamweaver - so it can't be that... I don't like it, though. I wish I knew how to get rid of it.

# xivoid at 8/25/2007 7:20 am cst

document.fors.[...].elements is null or not an object

# Steve at 2/20/2008 9:04 am cst

Thanks for this, I updated it to replace spaces with dashes and remove anything but alpha-numeric characters and dashes. Here's what it looks like...

function sectionWebSafe(original) { // replace spaces with dashes var originalarray = original.split(" ") var webSafe = originalarray.join("-") // remove special characters var originalarray = webSafe.split(RegExp("[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-]")) var webSafe = originalarray.join("") return webSafe.toLowerCase(); }

# Techlands at 10/10/2008 10:30 am cst

here is an updated one that strips anything not alphanumeric except hyphen

Suggest
# Techlands at 10/10/2008 10:34 am cst

Added strip whitespace at the front and end




bfc757512077c4a08711c99143ef0cc7






c936661a0476574a0893147cf12d4881





# Techlands at 10/10/2008 10:34 am cst
Suggest
# Gary McGill at 4/26/2009 12:56 pm cst

hi there can you help i am working on a form were a user add say a number in a box and when they click send it adds the number to the end of a url can this be done as i have tred every think i know

# Mark at 7/5/2009 6:59 am cst

Perfect! Exactly what I needed for exactly the same reason! Thanks Matt!