How to link to an anchor in an Iframe

Hi,

I am having problems linking to an anchor in an Iframe and I have not found any solutions yet that worked for me.

So this is my case:

All of this happens in the same domain.

(Page A) I have a page with content and an anchor

(Page B) Then I have a page with an Iframe of Page A

(Page C) And lastly a page where I have to link to the anchor in the Iframe

Does any of you tried this before or knows how his can be done with javascript?

Regards
Marcelo

Yes, you can set the location.hash of the iframe like you can with the main window:

const iframe = window.frames['my-iframe']

iframe.contentWindow.addEventListener('load', function () {
  this.hash = '#some-target'
})

When you link to that page you might pass the iframe-hash in a query string, like say

example.com/page-c.html?iframehash=%23some-target

then read that value in the JS and set the hash as shown above.

Thank you a lot m34p0p :slight_smile:

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.