In Adobe Experience Manager, "designer JavaScript" almost always means the JavaScript that runs inside a client library created in the classic Designer tool, usually a small script that reads page properties or configuration and builds links dynamically. To generate hyperlinks with it, you write a function that derives a target path, passes it through the page context’s link-externalizer, and sets the resulting href at the moment the page renders or a component initializes. The key condition: don’t hardcode external URLs in clientlib JavaScript if the author can define them; externalize them server-side or resolve them against the page context so that publish and author domains stay correct.

The trickier part is understanding which "designer JavaScript" context you’re actually in. AEM supports several places where a designer might drop script: legacy /etc/designs client libraries, page-level custom scripts in the page properties dialog, component dialogs using granite/ui/components/coral/foundation/clientlibs, or an HTL component with a backing clientlib. Each has different access to the page model, different timing, and different authoring constraints. This guide walks through the ordered steps for the common case (a clientlib script that builds a link from configuration) and then covers the edge cases: vanity URLs, link rewriting, multilingual paths, and the failures that silently produce a broken link instead of an error.

What "Designer JavaScript" Actually Refers to in AEM

Designer was the classic AEM tooling for creating client libraries before the modern front-end pipeline and the ui.frontend module. A Designer client library is a .js file placed under /etc/designs//js (or a clientlib folder that inherits from a design), with a .content.xml that declares categories and dependencies. When the page is rendered, the cq:includeClientLib tag or the HTL data-sly-call on the clientlib template includes those scripts in head or at the end of body.

That placement matters. A script in head runs before the DOM exists, so any link generation that touches the DOM either defers to DOMContentLoaded or uses a defer attribute on the script tag. Scripts placed at the bottom of body can run immediately against the crafted HTML. In practice, "generate a hyperlink" in Designer JavaScript is one of two design patterns:

A static link whose target is a path or page property: build the href string and inject it into a container element. A dynamic link whose target changes based on runtime input (a locale dropdown, a selected tab, a query parameter): build the href, then update it when the input changes.

Both need the same pieces: a reliable source for the target path, a correct base/context path, and a safe way to write the attribute. Missing any of these is where most "works on author, broken on publish" tickets come from.

Prerequisites Before You Write a Line of Script

Before writing script, confirm five things.

1: The component or page exposes the link target as a property. The target should live in a dialog field (page properties or component dialog), not in the JavaScript. Ask the backend developer which property, which resource type, and whether it is inherited. A common inheritance pattern is a page property like cq:redirectTarget or a custom jcr:title-adjacent string property. 2: The clientlib category is included on the page. Open the page’s HTML source and search for the category, e.g. myproject.designer. If it doesn’t appear, the cq:includeClientLib tag is missing or the design isn’t attached. 3: The page context is available. AEM’s client-side context is exposed via window.CQ and the granular cq:Page metadata, or in modern setups through a page model injected into a data attribute. Confirm which one your project uses by inspecting the page source for CQ or window.Granite. 4: The externalizer is configured. AEM’s Link Externalizer (com.day.cq.commons.Externalizer) converts a JCR path to a full URL using the correct domain for author, publish, or preview. If it is not configured, the externalizer falls back to a relative path, and you may see author URLs leak onto the publish site. 5: You know the intended domain and scheme. If links must always be HTTPS, or must respect a CDN domain, the target resolution must include that decision — either in the server-side model or in a documented script constant.

If any of these is missing, fix it first. A script that "generates a link" from an undefined property produces undefined in the href and a console error, which looks like a code bug but isn’t.

These steps assume you have a property named linkTarget on the page and a clientlib category myproject.designer included on the page. The same routine works for a component if you read the property from the component resource instead.

Step 1 — Confirm the property exists and is the intended one. In CRXDE Lite or the page properties dialog, verify linkTarget holds a value for the current page. Note whether it’s an absolute URL, a JCR path like /content/myproject/en/contact, or a vanity path. The script’s behavior must match that format.

Step 2 — Create or extend the Designer client library. Under /etc/designs//, create a js folder with a links.js file and a .content.xml declaring categories (e.g., myproject.designer) and dependencies (commonly cq.authoring.editor, if you plan to interact with editables). The exact dependency depends on whether you use the authoring layer at runtime.

Step 3 — Read the target from the page model. For a classic setup:

For a modern setup, the HTL component renders data-link-target="${pageProperties.linkTarget}", and the script reads it. Doing it this way keeps the JCR read server-side, which is faster and does not require exposing page properties to the browser.

Step 4 — Build the href with the externalizer. Never concatenate the domain manually. The externalizer’s client-side counterpart is not a single function in classic AEM; the supported approach is to render the externalized URL into the page via HTL (${externalizer.publishLink(resource.resolver, resource.path)}) and read it into the script, or to use the Granite.HTTP.externalize() helper. A call like Granite.HTTP.externalize('/content/myproject/en/contact.html') resolves the path against the current context and returns a URL that is correct for author or publish.

Step 5 — Set the attribute, not the inner HTML. Use element.setAttribute('href', href) or assign element.href. Avoid innerHTML with a template string; it invites XSS if the property ever contains markup.

Step 6 — Handle the missing-value case explicitly. If linkTarget is empty, do not render a link at all. Remove the anchor or set aria-disabled="true" and mark it visually, but do not leave href="". An empty href reloads the current page, which is one of the most confusing bugs for editors and users.

Step 7 — Verify with a simple check. Open the page on the publish instance (or as close as your local setup can approximate). Inspect the anchor: the href should be an absolute URL on the publish domain, not an internal path and not an author hostname. Click it in a new tab. Then repeat on a page where linkTarget is blank and confirm no anchor with an empty href appears.

Success signal: the generated anchor resolves to a 200 response and its href hostname matches the publish domain. Failure signal: the href contains undefined, contains /content/... unmodified when a full URL was expected, or points to localhost when viewed from another machine. Each points to a different step, and fixing in that order (property, inclusion, externalizer) is the fastest path.

Reading the Target From a Component Dialog Instead of Page Properties

The same pattern applies when the target is on a component rather than the page. The component’s HTL renders data-link-target="${properties.linkTarget}" and the script reads that attribute scoped to "the current component." The critical difference is scope: if a component is repeated (a list of cards), each instance needs its own script attachment or its own data- attribute, and the script must find its own target rather than the first one on the page.

A robust approach:

This loop handles one card or fifty. It also runs idempotently, so if the page re-renders part of the DOM, calling it again is harmless. That matters in AEM authoring because the authoring layer sometimes re-renders components after dialog edits.

Handling Vanity URLs Without Breaking Them

Vanity URLs are a well-known AEM trap for generated links. If a page has a vanity URL configured in its properties, the externalizer will return the vanity path when it resolves the page. A script that treats the target as a plain JCR path and prefixes it with the site root can double-prefix or miss the vanity. There are three defensible patterns.

Pattern A — let the server decide. Render ${externalizer.publishLink(resource.resolver, currentPage.path)} into the page and have the script use it verbatim. The server receives the current page’s resource resolver, and the externalizer’s vanity resolution is applied before the browser sees it.

Pattern B — resolve vanity paths deliberately. If the property is expected to contain a vanity URL, treat it as-is and do not externalize. Prepend the domain only when the string lacks a scheme and the property is a path that does not start with /content/. Document this rule in the component README; it is a config contract, not a code comment.

Pattern C — use a servlet endpoint. For dynamic links built at runtime (for example, a link that changes based on a dropdown selection), call a small JSON servlet that returns the externalized URL. The servlet uses the same externalizer and resolver server-side, so vanity handling is consistent everywhere. The script only sets the returned href. This is more work up front but eliminates the author/publish divergence.

Whichever pattern you pick, test with a page that has a vanity URL configured, and with a page that does not. If both pass, your generation logic is consistent.

The usual dynamic case is a link that must change when a visitor picks a locale, a region, or a tab. The script builds the href from a base path and a selected value. The risk is falling back to client-side string manipulation without the externalizer, which starts producing URLs that drift from the canonical structure.

A workable structure:

Render the base path into the page as a data- attribute, e.g. data-locale-base="/content/myproject/en". Attach a change handler to the locale selector. Build the candidate path by concatenating the base with the selected locale segment. Pass it to Granite.HTTP.externalize(), then update the anchor.

Two caveats. First, the externalizer’s behavior on a path that doesn’t exist in the repository depends on configuration; it will generally still produce a URL, so the script alone cannot confirm the target is valid. Validate the migration of locale paths at build time with a link checker. Second, if the site is served from a CDN with a different hostname, the externalizer may return an origin that is not the CDN domain; the CDN configuration must recognize that origin.

Check the result: switch locales and confirm the href changes to the expected full URL, not to a relative fragment. Then disable JavaScript in the browser and confirm the page still has a sensible default link (the server-rendered one), because JavaScript-generated links are invisible to crawlers that don’t execute scripts.

Pitfalls, Debugging, and the Stop Condition

The failure modes cluster around four causes. Diagnose in this order, because each is cheaper to check than the next.

Cause 1 — target source empty or misspelled. Confirm the property name in CRXDE matches the script. A property spelled linktarget versus linkTarget is a silent undefined. Check: view the rendered HTML and search for the data- attribute.

Cause 2 — externalizer returns a relative or wrong-host URL. Check the Link Externalizer OSGi configuration. On a dispatcher-fronted publish setup, the externalizer’s domain must match the public domain, not the internal publish host. Check: view source on a rendered page that uses server-side externalization and compare hostnames.

Cause 3 — script timing. The anchor exists after the script runs, usually because the component is rendered by a client-side framework or the script runs in head before the element is parsed. Check: move the script to defer or to the end of body, or wrap the logic in DOMContentLoaded.

Cause 4 — authoring context. AEM’s edit mode loads the page inside frames and re-initializes components. Scripts that bind once on load can bind twice on authoring re-render. Check: run the generator inside a guard that marks the element as processed.

Stop and escalate to the component’s backend owner when the link target is supposed to come from an integration (a translation service, a PIM, a CRM) and the rendered value is missing or wrong. That is not a scripting bug; the upstream model is producing bad data, and no client-side fix should paper over it.

You don’t have to generate links in the browser at all. If the target is known at render time, HTL can emit the final URL directly:

That approach is faster, accessibly rendered for crawlers and assistive tech, and eliminates the timing issues above. Use Designer JavaScript only when the link genuinely must change after load — locale switching, personalization after a third-party call, or progressive enhancement where the server-rendered fallback needs to be corrected. For anything authored statically, prefer the server-rendered link and skip the script. The trade-off is small but real: you lose the client-side flexibility, but you gain reliability and SEO-friendliness.

FAQ

Does Designer JavaScript still exist in current AEM versions?

The classic Designer tooling still works in AEM 6.5 and remains in AEM as a Cloud Service for legacy clientlibs, but Adobe’s recommended path for new front-end code is the ui.frontend module with a build pipeline. If you are starting fresh, use ui.frontend; if you are maintaining an existing /etc/designs clientlib, the pattern here applies unchanged.

Can I read page properties directly from Designer JavaScript?

Not reliably in the browser. Page properties are stored in the JCR and are not generally exposed as a public JSON endpoint. Render what you need into a data- attribute via HTL, or fetch it from a servlet. Reading troughs through /libs endpoints is fragile and will break on upgrades.

Almost always an externalizer configuration that returns the author host, or a script that references an authoring-only CQ object. The externalizer must be configured for the publish domain, and the script should guard for the CQ object’s absence rather than assuming it.

Only write the href when the element is explicitly marked as needing generation, e.g. data-generate-link="true". Leave all other anchors alone. That keeps the enhancement opt-in and avoids surprising authors.