
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
JavaScript code to find the coordinates of every link in a page
Following is the code to find the coordinates of every link in a page using JavaScript −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { font-size: 20px; font-weight: 500; } </style> </head> <body> <h1>Coordinates of every link in a page</h1> <div style="color: green;" class="result"></div> <a href="www.google.com">google.com</a> <a href="www.facebook.com">facebook.com</a> <a href="www.yahoo.com">yahoo.com</a> <button class="Btn">CLICK HERE</button> <h3> Click on the above button to display the link coordinates </h3> <script> let resEle = document.querySelector(".result"); let BtnEle = document.querySelector(".Btn"); let links = document.getElementsByTagName("a"); BtnEle.addEventListener("click", () => { for (let i of links) { console.log(i); resEle.innerHTML += "X axis = " + i.offsetWidth + " Y axis = " + i.offsetHeight + "<br>"; } }); </script> </body> </html>
Output
The above code will produce the following output −
On clicking the ‘CLICK HERE’ button −
- Related Articles
- How to write inline JavaScript code in HTML page?
- Highlight a text, every time page loads with JavaScript
- Calling asynchronous code from index.html page in JavaScript?
- JavaScript code to de-select text on HTML page.
- How to link jQuery in HTML page?
- How to find the href attribute of a link in a document in JavaScript?
- How to find the coordinates of the cursor with JavaScript?
- Creating a QR Code of a link in React JS
- How to specify the URL of the page the link goes to in HTML?
- How to scroll a Web Page using coordinates of a WebElement in Selenium WebDriver?
- How to find the href and target attributes in a link in JavaScript?
- How to avoid Java code in jsp page?
- How to find the coordinates of the cursor relative to the screen with JavaScript?
- How to get the value of the id attribute a link in JavaScript?
- How to search the value of the target attribute of a link in JavaScript?

Advertisements