cd /news/generative-engine-optimization/ai-answers-link-to-the-exact-sentenc… · home › topics › generative-engine-optimization › article
[ARTICLE · art-177647] src=markodenic.tech ↗ pub= topic=generative-engine-optimization verified=true sentiment=· neutral

AI answers link to the exact sentence. Your site can too

A Web Dev Weekly post published November 28, 2026 shows developers how to add a "Copy link to selection" button that turns highlighted text into a URL using the browser's text fragment syntax, such as `#:~:text=only%20the%20first%20request`, so readers can share a link that scrolls to and highlights an exact sentence. The post's JavaScript snippet encodes selections with `encodeURIComponent`, replaces hyphens with `%2D`, and shortens long passages to a start/end pair of four words each, while a `mousedown` handler prevents the click from clearing the reader's selection before it is read. The technique matters because AI answers and search engines already link to specific sentences, and the post notes every major browser supports text fragments even though few readers know Chrome's and Edge's built-in "Copy link to highlight" option exists.

key takeaway

Build text-fragment links with `#:~:text=start,end` using the first four and last four words of a selection, encode with `encodeURIComponent` plus `%2D` for hyphens, and call `preventDefault()` on `mousedown` so the click doesn't clear the selection.

by read5 min views7 publishedNov 28, 2026

AI answers link straight to the sentence they quote. Your browser can make these links too. Add one button so your readers can share them.

Web Dev Weekly November 28, 2026

AI answers link to the exact sentence. Your site can too

AI answers link straight to the sentence they quote. Your browser can make these links too. Add one button so your readers can share them.

Built something and want to sell it?

CREEM costs nothing to start: no monthly fees, no setup fees, and you only pay when you earn. It also handles sales tax and VAT in 190+ countries, so you don't have to.

Someone wants to send a colleague one sentence from your 3,000 word article. They copy the URL, paste it, and add “it’s somewhere in the middle, search for ‘cache'”. The colleague opens the page at the top and gives up.

Search engines and AI answers already solved this. Their source links often end in something like #:~:text=only%20the%20first%20request, and the browser opens the page, scrolls to that sentence and highlights it. This is called a text fragment, and every major browser supports it.

Your browser can even make these links for you. In Chrome or Edge, select any text, right-click and pick “Copy link to highlight”. Almost nobody knows it’s there, so your readers will never find it. Put a visible button next to your content instead.

The fix #

Add a button to your article template:

<button class="quote-link" type="button">Copy link to selection</button>

Then turn the selected text into a link and copy it:

const encode = (text) => encodeURIComponent(text).replace(/-/g, '%2D');

function linkToSelection() {
  const lines = getSelection().toString().split('\n').map((l) => l.trim()).filter(Boolean);
  if (!lines.length) return null;

  const first = lines[0].split(/\s+/);
  const last = lines.at(-1).split(/\s+/);
  const text = lines.length === 1 && first.length <= 8
    ? encode(lines[0])
    : `${encode(first.slice(0, 4).join(' '))},${encode(last.slice(-4).join(' '))}`;

  return `${location.origin}${location.pathname}#:~:text=${text}`;
}

const button = document.querySelector('.quote-link');
button.addEventListener('mousedown', (event) => event.preventDefault());
button.addEventListener('click', async () => {
  const url = linkToSelection();
  if (!url) return;
  await navigator.clipboard.writeText(url);
  button.textContent = 'Link copied';
});

The mousedown line stops the click from clearing the reader’s selection before you read it.

Caching is the cheapest performance win most sites never take. A response that is reused from the browser cache costs zero network requests, zero server time and zero bytes.

The catch is invalidation. Put a version number or a content hash in every asset's file name, and you can cache it for a year without ever serving a stale file.

HTML is the exception. Keep it short-lived, so a new deploy reaches readers on their next visit.

Select a sentence above (or across two paragraphs), then click the button.

Select a sentence in the demo, copy the link, then open it in a new tab.

#:~:text=start
#:~:text=start,end
#:~:text=prefix-,start,-suffix
  • text=start: highlight the first match of this text.
  • text=start,end: highlight everything from start toend . The fix uses this for long selections, so a whole paragraph becomes a short URL of eight words.
  • prefix- and -suffix: text right before or after the match, for when the same phrase appears more than once. Most quotes don’t need them.
  • &text=: repeat it to highlight several passages in one link.

Matching ignores case. Each piece of text has to sit inside one block, like one paragraph or one list item, which is why the fix takes the start from the first selected line and the end from the last.

Everything goes through encodeURIComponent, which handles spaces, commas and &. But it leaves - alone, and a dash in your quote would be read as a prefix or suffix marker. That is the .replace(/-/g, '%2D'). Skip it and any quote with “built-in” or “real-time” opens at the top of the page.

Style the highlight #

Browsers use their own highlight color by default. Match your brand instead:

::target-text {
  background: #fde68a;
  color: inherit;
}

The rough edges #

  • Whole words only: a match has to start and end on a word boundary. If someone selects half a word, the link opens at the top of the page instead.
  • Edits break old links: if you rewrite the sentence, the link stops matching and the page opens at the top. Nothing breaks, it just loses the scroll.
  • Older browsers do the same: no highlight, just the normal page. The link still works.
  • Not inside iframes: the browser only searches the main page, so quoted text must not live in an embedded frame.

Support: Chrome and Edge since 2020, Safari 16.1+, Firefox 131+. ::target-text styling is a bit newer: Chrome 89+, Firefox 131+, Safari 18.2+.

Where this comes up #

  • Long articles and docs: “see this exact paragraph” instead of “scroll down a bit”.
  • Terms, policies and changelogs: support teams can link the exact clause a customer asked about.
  • Help centers: answers that point to the one step that matters.
  • Your own sharing: combine it witha native share button and readers can send the quote straight to a chat app on mobile.

Why it matters #

  • No IDs needed: unlike#heading anchors, any sentence on any page is linkable, with zero changes to your markup.
  • Readers become citers: every quote they share sends someone to the exact part of your page that convinced them.
  • It’s just a URL: no backend, no storage. Likequery params , the state lives in the link itself.

Add the button to your article template, select your favorite sentence, and send the link to a friend.

Happy coding!

Marko

── more in #generative-engine-optimization 4 stories · sorted by recency
── more on @web dev weekly 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/ai-answers-link-to-t…] indexed:0 read:5min 2026-11-28 · —