Archive

Posts Tagged ‘nice uri’

Javascript get a nice uri from string


This function will transform a string into a nice uri.

You can use this to get a good level seo uri for all your links


function getNiceURI(_dirtyUri)
{
_dirtyUri = _dirtyUri.toLowerCase();
if(_dirtyUri === "") return _dirtyUri;

var encodedURI = encodeURIComponent(_dirtyUri);
var niceURI = encodedURI.replace(/[%20]/g,”_”);
niceURI = niceURI.replace(/[^a-zA-Z0-9-_]/g, ‘_’);

//Elimino eventuali underscore dal primo carattere
while(niceURI.indexOf(“_”) === 0) {
niceURI = _dirtyUri.substring(1, niceURI.length);
}

//Deleting underscores from beginning and end string
while(niceURI.lastIndexOf(“_”) === niceURI.length – 1) {
niceURI = niceURI.substring(0, niceURI.length – 1);
}

//Take off repeated underscores
while(niceURI.indexOf(“__”) !== -1) {
niceURI = niceURI.replace(“__”, “_”);
}

return niceURI;
}

Advertisements
%d bloggers like this: