How to Click Link Using JavaScript18 Mar 2025 | 4 min read To click a link on a web page, we need to trigger a user action on the click event. But what if we want to click a link pro grammatically using JavaScript without a user event or any other phenomenon? We could accomplish this using JavaScript. There may be multiple reasons why users want to click a link. Following are some scenarios where we require to click a link programmatically.
There may be several other reasons to perform the link clicking using JavaScript. The logic behind this functionality is that we will get the element using any of the DOM selector methods and apply the click() function to it. Consider the below example: The Script will be as follows to trigger the click functionality: Let's understand its different scenarios: On Page Load Redirect After DelayWe may require to redirect a user to another page when a page is loading. In such case, the above approach will be helpful. Here, we will redirect a user to another page at the page load time. A countdown will begin at page load. When the countdown is completed, a link will be clicked. Below is the implementation of this: Example: In the below example, we are taking the countdown timer for 1s. Let's consider the below HTML code: We will get the above element using the getElementById method. We do not have any text in this example, so it will not be visible to the user. Here, the link will be clicked with JavaScript; we will not take any user action. Below is the JavaScript code for this: The complete code of the above example is: In the above example, when the page is loaded, the immediate Script is called having the setTimeout function, which will execute the function redirect after one second. It will redirect the user to another page. When the redirectTo function is launched, it will target the anchor tag by its id and apply a click() function. Display a message and click on a link in JavaScriptLet's consider an other example where we need to show some message to the users on a specific user action and redirect them to another page. For example, if a user booked any product when it is successfully booked, we need to redirect him to another page. There may be many scenarios for which we need to implement this functionality. Let's understand it with a simple example. For example, we have a div, and we want to display a message to the user and then redirect him to another page. We will be performing it in the following three parts:
Below is the code for the link: From the above code, we are getting the DOM element using id value . There is no text in the above tag, so that it will be invisible to DOM. So, for that, we need to add the div element to DOM. In the below code, we have a div where we will get the user action and allow him to trigger some function by clicking. In the above code, we are applying cursor-pointer CSS to change the cursor type to look like the user has a click event. So, when we click on the div element, it will call the redirectTo function. Let's write the JavaScript code for the functionality to display the message and redirect it to another page. Below is the JavaScript code. Collectively, we can use the below code: The above example will display an alert message "You have clicked on div". Once we click on the OK button, it will redirect the user to the specified link. Though alert() is optional, you can use any other way to display the message, like in a modal box or a toaster and so on. Summary:We click a link pro grammatically using JavaScript without user event or any other phenomenon. This could be useful in various cases, such as responding to another user action or event, creating an automatic redirector using the link, delaying loading the next page upon a normal click or tap on a link, or downloading the server response in some Excel or CSV file. There could be many other cases than these cases. We can accomplish those cases and other similar requirements by using JavaScript. We have discussed a few use cases in this example. They can be helpful in providing the idea and logic for similar examples. It was a simple logic having a link with the id attribute and without the text so that it would not be visible to the DOM, and getting the value from its id and applying the click() function on it. We can have many other methods to perform the same action, like we can use any other DOM method or can choose the event listeners to trigger the event. But, using id, it was an easy way to perform the task. Next TopicJavaScript FormData |
The unique feature of async generator functions in javascript is their support to both await and yield. It returns an async iterator rather than a promise or iterator. The async generator functions are distinct from async functions and the generator functions in javascript. The () function...
3 min read
In this chapter, you will learn how to generate random images on a webpage with the help of JavaScript. So, we will create different random image generator using JavaScript and HTML source code. Firstly, it requires an array to contain the URLs of images to...
8 min read
Capitalizing the first letter of a string is a common task when working with text data in JavaScript. Whether you're formatting a title, heading, or just trying to make your text more visually appealing, capitalizing the first letter of a string can make a big...
3 min read
? A dynamic table is one whose number of rows varies based on the input it receives during runtime. Some websites or online programs, such as business websites, require the dynamic creation of a table while adding data or information. It can be done because JavaScript...
3 min read
When the mouse cursor passes on the HTML element, the onmouseover event is worked and shows user-defined functionality. This function also works when the mouse cursor selects the user element, html tag, or required data. When a user moves the cursor away from an element, the...
5 min read
The Element interface's insertAdjacentHTML() method converts the given text to HTML or XML and puts the generated nodes into the DOM tree at a predetermined point. To insert text as an HTML file at a certain point in the DOM, use the insertAdjacentHTML() method. The text...
4 min read
A collection of properties is stored in an object. In an object, there is an association of a name, also called a key and a value combined with called key and value pairs. But then, external dependency is not required by it when it refers to...
2 min read
? A beep sound is usually used for the notification alert in websites. There may be some other cases where you need to use a beep sound in your website. You can find several real-life examples of this beep sound, such as in grocery stores, libraries, and...
5 min read
The iterate the object data using JavaScript keys. It helps to get particular datasets data with a unique number. Iterating over an object can be accomplished in two ways, as discussed below: Method 1: Using a for...in the loop Method 2: Object.entries() map Method 1: Using a for...in the...
3 min read
The mouse rolls up or down the web page and starts to operate the mouse wheel function using the "onwheel" javascript. We can use the "onmousewheel" event, but it deprecates in the javascript. We can use the javascript for the "onwheel" attribute instead of the "onmousewheel" event. Syntax The...
3 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India