Amit Goyal
Amit Goyal

Reputation: 1

I want to convert long url to short url in last having ... how to do that

I want to convert long url to short in last having ... dots. For example this url

This is long url text

Now, Short url text

The code available long url to short url in above link. I am trying hard work for this. But cannot possible to make the url. I don't want to any API for url shortner. Just, using javascript.

Upvotes: -1

Views: 153

Answers (1)

fsdev
fsdev

Reputation: 36

I assume you want to shorten the url only when displayed, not as a href value. You can do it using the following function:

// function ensuring that return value won't be longer than max, including ellipsis (...)
function shorten(text, max = 30) {
    return (text && text.length > max) ? (text.substring(0, max - 3) + "...") : url;
}

Here is a way to test shorten()

const url = "https://mozodeals.com/threads/iqoo-12-5g-worlds-fastest-processor-snapdragon%C2%AE-8-gen-3-12gb-ram-and-256gb-rom.19/";

function createLink(url) {
    document.write(`<a href=${url}>${shorten(url, 20)}</a>`);
}

createLink(url);

I hope that is what you were looking for

Upvotes: 0

Related Questions