This is the modern approach:
Generate dynamic anchor
function createAnchor(url) { const anchor = document.createElement('a'); anchor.href = url; return anchor; }
Simulate click function
function simulateClick(element) { const event = new Event('click'); element.dispatchEvent(event); }
Implementation
const myAnchor = createAnchor('https://www.example.com'); simulateClick(myAnchor);