Don’t worry mate I have got you covered. The substring() method extracts characters from the string, but it uses two arguments: the start index and the end index (not inclusive).
var pathname = document.referrer;
var truncatedPathname = pathname.substring(0, 20); // Truncate to 20 characters
document.getElementById("foo").innerHTML = "<a href='" + pathname + "'>" + truncatedPathname + "</a>";
I hope you like it.