Outreach Database

International SEO

Posts by category


Related

blog home >JavaScript function to generate URLs without any rubbish characters

JavaScript function to generate URLs without any rubbish characters

Written by

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.
 



7 comments

swbratcher
Which does google prefer? The underscore "_" or dash "-" characters splitting words. Or does it not matter? Scott

Frank Orchid Box SEO team
Hi Scott, Google gives different opinions about it. I like to use _ as - could be used to link words in the English language. If you use _ you are pretty much safe. The important part is to separate keywords and avoid this : JavaScriptfunctiontogenerateURLswithoutanyrubbishcharacters Search engine can't really extract keywords from long strings.

Repo Depot Toronto
I find myself coming to your blog more and more often to the point where my visits are almost daily now!

swittyfleenty
How do you play Barcelona in this year?

anzelliiu
Эротический: детский видео чат, интим видео чат бесплатно, секс чат оренбург, детский чат знакомств, чат без регистрации для лезбиянок, чат без регистрации leenex, видео чат для двоих, как сделать онлайн чат. Сайт chat.24lux.ru

Frubpoepe
who thinks that - DGG better to rest in the winter? [url=http://viagravillage.com/]buy v i a g r a from canada[/url]


Write something