Recently I needed to create a SEO safe URL string for an ASP.Net Blog application I was coding. I wanted the title field of the form to be transformed into a SEO friendly URL, then copy that string to another field for storing the SEO friendly string.

I came up with the following JavaScript function that will do just that.

function setURLName(textBox, e, boxtochange) {
  var textValue = textBox.value;
  var checkURL = document.getElementById(boxtochange).value;
  if (checkURL == "") {
    document.getElementById(boxtochange).value = textValue.replace(/[^\w\s]/gi, '').replace(/\s/g, '-');
  }
}

You then need to add this event to the field you want to transform:

onblur="setURLName(this, event, 'PageURL')"

PageURL is the ID of the field to send the SEO friendly string to.

That's all there is to this one.

 

Categories


Most Visited