The goal of the function is to see if the length of the given string is greater than the given maximum string length ( num ). If it is, truncate the string right at the maximum length and return it with an ellipsis (…) at the end. If the string is shorter or equal to the cutoff string length, return the string as is.
In JavaScript, trunc() is a function that is used to return the integer portion of a number. It truncates the number and removes all fractional digits. Because the trunc() function is a static function of the Math object, it must be invoked through the placeholder object called Math.
The slice() method extracts a part of a string. The slice() method returns the extracted part in a new string. The slice() method does not change the original string. The start and end parameters specifies the part of the string to extract.
Use the substring method:
var length = 3;
var myString = "ABCDEFG";
var myTruncatedString = myString.substring(0,length);
// The value of myTruncatedString is "ABC"
So in your case:
var length = 3; // set to the number of characters you want to keep
var pathname = document.referrer;
var trimmedPathname = pathname.substring(0, Math.min(length,pathname.length));
document.getElementById("foo").innerHTML =
"<a href='" + pathname +"'>" + trimmedPathname + "</a>"
Here's one method you can use. This is the answer for one of FreeCodeCamp Challenges:
function truncateString(str, num) {
if (str.length > num) {
return str.slice(0, num) + "...";
} else {
return str;
}
}
yes, substring. You don't need to do a Math.min; substring with a longer index than the length of the string ends at the original length.
But!
document.getElementById("foo").innerHTML = "<a href='" + pathname +"'>" + pathname +"</a>"
This is a mistake. What if document.referrer had an apostrophe in? Or various other characters that have special meaning in HTML. In the worst case, attacker code in the referrer could inject JavaScript into your page, which is a XSS security hole.
Whilst it's possible to escape the characters in pathname manually to stop this happening, it's a bit of a pain. You're better off using DOM methods than fiddling with innerHTML strings.
if (document.referrer) {
var trimmed= document.referrer.substring(0, 64);
var link= document.createElement('a');
link.href= document.referrer;
link.appendChild(document.createTextNode(trimmed));
document.getElementById('foo').appendChild(link);
}
Following code truncates a string and will not split words up, and instead discard the word where the truncation occurred. Totally based on Sugar.js source.
function truncateOnWord(str, limit) {
var trimmable = '\u0009\u000A\u000B\u000C\u000D\u0020\u00A0\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u2028\u2029\u3000\uFEFF';
var reg = new RegExp('(?=[' + trimmable + '])');
var words = str.split(reg);
var count = 0;
return words.filter(function(word) {
count += word.length;
return count <= limit;
}).join('');
}
Thought I would give Sugar.js a mention. It has a truncate method that is pretty smart.
From the documentation:
Truncates a string. Unless split is true, truncate will not split words up, and instead discard the word where the truncation occurred.
Example:
'just sittin on the dock of the bay'.truncate(20)
Output:
just sitting on...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With