Log in to share your answer and help other learners.
Log in to answerBest Answer · By JanBask Salesforce Expert
Answered on Jun 6, 2024
To trigger a javascript href in LWC, you just need to use innerHTML.
import { LightningElement } from "lwc";
export default class App extends LightningElement {
renderedCallback() {
const content = 'Google';
this.template.querySelector('div')[removed] = content;
}
}
Or, if you mean to say you want to redirect to a website:
[removed].href = 'https://www.google.com/';
Or, if you want to trigger a download:
const a = document.createElement('a');
a.href = 'https://www.google.com';
document.body.appendChild(a); // Firefox apparently requires this
a.click();
document.body.removeChild(a);
Best Answer · By JanBask Salesforce Expert
Ranjana Admin JanBask Expert
Answered on Jun 6, 2024
In Salesforce Lightning Web Components (LWC), you can trigger a href link using JavaScript. This can be done by dynamically creating and dispatching a click event on an anchor () element. Here's a step-by-step guide on how to achieve this:
Step-by-Step Guide
Create the LWC Component:
1. First, create a new Lightning Web Component. For example, let's name it navigateLink.
2. Component HTML Template (navigateLink.html):
In the HTML file, you don't need to have the actual element visible if you are dynamically creating it through JavaScript. However, you can have a button or any other trigger element.
3. Component JavaScript Controller (navigateLink.js):
export default class NavigateLink extends LightningElement { handleNavigate() { // Create a new anchor element const anchor = document.createElement('a'); // Set the href attribute to the desired URL anchor.href = 'https://www.example.com'; // Set the target attribute if you want to open in a new tab anchor.target = '_blank'; // Append the anchor to the document body document.body.appendChild(anchor); // Trigger the click event on the anchor element anchor.click(); // Remove the anchor from the document body document.body.removeChild(anchor); }}CSS (navigateLink.css):
You might not need CSS specifically for this functionality, but if you do, create the CSS file accordingly.
/* Example: Optional styling if needed */Explanation
Usage
1 Button Click: When the button is clicked, the handleNavigate method is called, which handles the creation and triggering of the anchor element's click event.
This approach ensures that you can dynamically navigate to any URL using JavaScript within an LWC component, adhering to the best practices of Salesforce Lightning Web Components.
Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.
Step-by-step Salesforce guides from industry experts
Common Salesforce interview questions, answered
Guides, tips and career advice on Salesforce from JanBask experts.
Salesforce How to Become a Salesforce Administrator in 2027: A Step-by-Step Career Roadmap
Want to become a Salesforce Administrator? Get the complete 2026 roadmap skills, certification, salary & career path. Start your…
Salesforce Salesforce Admin Self Study vs Live Training: How to Choose the Right Path
Compare Salesforce Admin self-study and live training by cost, duration, flexibility, mentorship, and career support. Find the…
Salesforce How a Salesforce Admin Certification Shapes Your Career Growth
Wondering if a salesforce admin certification is worth it? See how it shapes hiring, salary, and long term salesforce admin…
Salesforce Salesforce Admin vs Developer Salary: Which Path Actually Pays More?
Salesforce admin vs developer salary: developers earn 15–25% more. See entry, mid and senior pay, why the gap exists, and how…