Domen Lombergar
Mar 17th 2008, 3:24 pm
hey everyone.
I have a bit of a problem - i'm using javascript to automatically generate web-friendly urls from the titles, but i've run into some issues with special characters
This is the code:
function setAlias(objvalue){
document.newpage.newpagealias.value = makesafe(objvalue);
}
function makesafe(text){
text = text.replace(" / ","_");
text = text.replace("'","");
text = text.replace("\"","");
text = text.replace("/","_");
text = text.replace("'","_");
text = text.replace(" - ","_");
text = text.replace(" ","_");
text = text.replace("è","c");
text = text.replace("š","s");
text = text.replace("ž","z");
text = text.replace("È","C");
text = text.replace("Š","S");
text = text.replace("Ž","Z");
text = text.replace( "&", "and");
text = text.replace( "%", "Percent");
text = alphanumeric(text);
text = text.replace("____","_");
text = text.replace("___","_");
text = text.replace("__","_");
text = text.toLowerCase();
return text;
}
function alphanumeric(userval)
{
for(var j=0; j<userval.length; j++)
{
var eachChar = userval.charAt(j);
var hh = eachChar.charCodeAt(0);
if((hh > 47 && hh<59) || (hh > 64 && hh<91) || (hh > 96 && hh<123) || (eachChar =="_") || (eachChar =="-"))
{
//
}
else
{
userval = userval.replace(eachChar,"_");
}
}
return userval;
}
The two input fields that are connected here are the following two:
<input type="text" class="text" name="newpagetitle" value="" size="56" maxlength="255" onchange="setAlias(this.value)" />
and
<input type="text" class="text" name="newpagealias" value="" size="16" maxlength="50" />
basically it should convert all čšžČŠŽ into cszcsz but it does this only for the first of them, then turns the rest into _
So my question is basically...
Does anyone know what would need changing here so that this would work in converting all special characters into regular letters
and ... does anyone know if it's also possible to remove the ending underscore (example from "word_" to "word")?
Thanks so much!
I have a bit of a problem - i'm using javascript to automatically generate web-friendly urls from the titles, but i've run into some issues with special characters
This is the code:
function setAlias(objvalue){
document.newpage.newpagealias.value = makesafe(objvalue);
}
function makesafe(text){
text = text.replace(" / ","_");
text = text.replace("'","");
text = text.replace("\"","");
text = text.replace("/","_");
text = text.replace("'","_");
text = text.replace(" - ","_");
text = text.replace(" ","_");
text = text.replace("è","c");
text = text.replace("š","s");
text = text.replace("ž","z");
text = text.replace("È","C");
text = text.replace("Š","S");
text = text.replace("Ž","Z");
text = text.replace( "&", "and");
text = text.replace( "%", "Percent");
text = alphanumeric(text);
text = text.replace("____","_");
text = text.replace("___","_");
text = text.replace("__","_");
text = text.toLowerCase();
return text;
}
function alphanumeric(userval)
{
for(var j=0; j<userval.length; j++)
{
var eachChar = userval.charAt(j);
var hh = eachChar.charCodeAt(0);
if((hh > 47 && hh<59) || (hh > 64 && hh<91) || (hh > 96 && hh<123) || (eachChar =="_") || (eachChar =="-"))
{
//
}
else
{
userval = userval.replace(eachChar,"_");
}
}
return userval;
}
The two input fields that are connected here are the following two:
<input type="text" class="text" name="newpagetitle" value="" size="56" maxlength="255" onchange="setAlias(this.value)" />
and
<input type="text" class="text" name="newpagealias" value="" size="16" maxlength="50" />
basically it should convert all čšžČŠŽ into cszcsz but it does this only for the first of them, then turns the rest into _
So my question is basically...
Does anyone know what would need changing here so that this would work in converting all special characters into regular letters
and ... does anyone know if it's also possible to remove the ending underscore (example from "word_" to "word")?
Thanks so much!