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

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

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

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by herminia_bruen , 6 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.

Related Threads:

How to draw coordinates in javascript canvas?
How to get parent iframe id in javascript?
How to get point coordinates in chart.js ****ter plot?
How to open a iframe link within the same iframe?
How to update an iframe with javascript?
How to bypass http link to https from an iframe?