I guess you may have heard about the slice()
method — using this, we can extract a portion of a string based on the index positions provided.
Now, if you want to keep the first N characters and truncate the rest, you can use this approach and let your worries about boundaries be tossed away:
javascript
CopyEdit
var pathname = document.referrer;
var truncatedPathname = pathname.slice(0, 20); // Truncate to 20 characters
document.getElementById("foo").innerHTML = "<a href='" + pathname + "'>" + truncatedPathname + "</a>";
Simple, clean, and gets the job done!