JavaScript Function To Generate URLs Without Any Rubbish Characters
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 a collection of amazing JS functions 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 amazing CMS please get in touch.
Related reading…
Artificial Intelligence is changing Web analytics forever.
As the world of business becomes increasingly complex, finding solutions to problems in the office is becoming more crucial. Artificial intelligence (AI) technologies have stepped in to give us a helping hand, and are quickly transforming almost...
read moreThe Orchid Box Adwords Raspberry Pi Display Bot
The most important thing to bear in mind when conducting local keyword research is that one should never translate keywords, but ideas. It is best to do keyword research for local markets and then use the keywords in the translated content....
read moreInject Google Analytics Client ID to All Your Links
We have been playing with Google Analytics Client ID and found that not many agencies have been using it. The Client ID can be found in the GA cookie and Client ID is stored on the GA cookie of your website’s actual domain. Google Analytics stores...
read more