Have you ever wanted to generate perfect URL from blog post or any page title?

We couldn't find any free function available, for this reason our Sergey built an collection of amazing JS function for Orchid box CMS. We have decided to release it to the world so no other developer should struggle as we did.

URL stripper features:

-          Strip of any non alphanumeric characters

-          Space converted to _

-          Not more than one _ between alphanumeric characters

-          JS function can be invoked on focus, hence very fast

The function : GenerateUrl  would transform :

e.g. 1

source: this is my title

result: this_is_my_title

e.g. 2

Source: This is my n.1 post, really ? Should I have written more          , or     less    ?

Result: This_is_my_n_1_post_really_Should_I_have_written_more_or_less

E.g. 3

Source: rubbish characters : &^%&^%&%*^(**&^%"£%"££^%$&^%^& (I am not rubbish)

Result: rubbish_characters_I_am_not_rubbish 

<script type="text/javascript">

 

  function trim(s) {

                var l = 0; var r = s.length - 1;

                while (l < s.length && s[l] == ' ')

                { l++; }

                while (r > l && s[r] == ' ')

                { r -= 1; }

                return s.substring(l, r + 1);

            }

     

      function looseRepeatedCharacter(string, character)

      {

          var res = '';

          var prevChar = '';

 

          if (string && string.length > 0) {

 

              for (i = 0; i < string.length; i++) {

                  var allow = false;

 

                  if (prevChar == '') {

                      allow = true;

                  }

                  else {

                      if (prevChar == string[i]) {

                          if (character != prevChar) {

                              allow = true;

                          }

                      }

                      else {

                          allow = true;

                      }

                  }

                  if (allow) {

                      res = res + string[i];

                  }

                  prevChar = string[i];

              }

              if (res.length > 0 && res[res.length - 1] == character) {

                  res = res.substring(0, res.length - 1);

              }

          }

         

          return res;

      }

     

      function GenerateUrl(sender) {

          if (sender) {

              var url = trim(sender.value);

 

              if (url == '') {

                 

                  var title = document.getElementsByName('title');

                  if (title) {

                      url = trim(title[0].value);

                      if (url != '') {

                          var test = new RegExp("[^0-9a-zA-Z]", "g");

                          //replace special characters

                          url = url.replace(test, "_");

                          //get rid of repeating '_'

                          sender.value = looseRepeatedCharacter(url, '_');

                         

                      }

                  }

              }

          }

      }

 

</script>

Usage example:

<input type="text" name="url" class="input_95" onfocus="GenerateUrl(this)"/>

Happy amazing URLs !

... and if you want to know more about our amaizing CMS please get in touch.