Here is some code that shows you how you can click a link with JavaScript (without using the mouse):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="" /> <title>Click a link with JavaScript</title> <style type="text/css"> * { margin: 0; padding: 0; border: 0; } </style> </head> <body> <a id="myLink" href="https://www.bennyn.de/">Hyperlink</a> <script type="text/javascript"> function clickLink(id){ var theEvent = document.createEvent("MouseEvent"); theEvent.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); var element = document.getElementById(id); element.dispatchEvent(theEvent); while (element){ if (element.tagName == "A" && element.href != ""){ if (element.target == "_blank") { window.open(element.href, element.target); } else { document.location = element.href; } element = null; } else{ element = element.parentElement; } } } clickLink('myLink'); </script> </body> </html> |