How to get coordinates of a link in iframe in javascript?

by tressie.damore , in category: HTML & CSS , 2 months ago

How to get coordinates of a link in iframe in javascript?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by herminia_bruen , 2 months ago

@tressie.damore 

To get the coordinates of a link inside an iframe using JavaScript, you can follow these steps:

  1. Get the iframe element using JavaScript:
1
var iframe = document.getElementById('yourIframeId');


  1. Get the content document of the iframe:
1
var iframeDoc = iframe.contentDocument || iframe.contentWindow.document;


  1. Get the link element inside the iframe:
1
var link = iframeDoc.getElementById('yourLinkId');


  1. Get the coordinates of the link element:
1
2
3
4
5
var linkRect = link.getBoundingClientRect();
var linkCoordinates = {
  top: linkRect.top + window.pageYOffset,
  left: linkRect.left + window.pageXOffset
};


Now, the linkCoordinates object will contain the top and left coordinates of the link element inside the iframe.