JavaScript string replace for post slug Aug22 '05
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:
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.
Categories: JavaScript
, Tutorials ![]()
Add Feedback (view all)
Leave feedback
Have you been getting any spam through your contact form? Ever since I added the security check, spa ... Read more.
document.fors.[...].elements is null or not an object ... Read more.
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. ... Read more.
matthom
is published and produced by Matt Thommes - an independent publishing enthusiast, mobile blogger, content creator, informative writer, web developer from a suburb of Chicago.
Never one to conform, Matt intends to promote the effect the web has on our lives, in an effort to intensify, instruct, and clarify all that is happening around us.
Popular Pages
- Fast rounded corners in Photoshop (7873 recent visits)
- PHP – passing variables across pages (2857 recent visits)
- JavaScript set selected on load (2345 recent visits)
- Removing all child nodes from an element (1704 recent visits)
- iPod songs out of order? (1412 recent visits)
- Firefox 3 smart address bar: wildcard search (1300 recent visits)
- Britney - Everytime piano tab (1180 recent visits)
- MySQL LEFT JOIN syntax (971 recent visits)
- Breathe Me - Sia (824 recent visits)
- Tumblr: how blogging should be (727 recent visits)
Similar Entries
- JavaScript badges should reside on the bottom of the HTML page (27 recent visits)
- JavaScript: billing/shipping address copy (29 recent visits)
- Using JavaScript to obtain data from HTML and XML (62 recent visits)
- JavaScript set selected on load (2345 recent visits)
- JavaScript can access hidden HTML (125 recent visits)
- JavaScript open links in new window (426 recent visits)
Stats
334 unique visits since August 2008
Recent Referrers (click)
- javascript string replace alphanumeric
- javascript string replace function
- javascript string replace quote
- photoshop javascript string replace
- http://matthom.com/archive/200
- javascript string style
- document.forms in javascript replace
- replace javascript function wild card
- javascript replace
- javascript remove alpha from field
- javascript string replace value in text field
- javascript remove dashes from text
- string javascript replace
- replace space with enter
- javascript check for spaces and replace
- javascript + replace + alpha
- javascript string replace
- javascript remove spaces double
- javascript post
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 jav ... Read more.