0

I am trying to get the url of iframe after a redirection.

I tried below given codes

document.getElementById("content-preview").contentWindow.location.href;
document.frames['content-preview'].location.href;
$('iframeId').contents().get(0).location.href;

But none worked. All these returning the initial src url only. The parent window and iframe are in same domain only.

Please let me know if there is a solution for this.

I referred This question to reffer.

Arun
  • 3,640
  • 7
  • 44
  • 87
  • 1
    try `document["your frame name"].location.href` – Saadi Toumi Fouad Aug 24 '20 at 13:17
  • 1
    Assuming the content of the iframe is on the same domain as the parent window, what you have should work. If it's not on the same domain what you're attempting to do is not possible – Rory McCrossan Aug 24 '20 at 13:22
  • @SaymoinSam, Thanks for the effort. But it is giving me error "Uncaught TypeError: Cannot read property 'href' of undefined". I am using the iframe name here – Arun Aug 24 '20 at 13:26
  • @RoryMcCrossan, Yeah. I saw same answer in many places. but it is giving the initial src value even after the redirect – Arun Aug 24 '20 at 13:27
  • @Arun well that's weird, here is an example in my free website it's just testing https://saymoinsam4ever.000webhostapp.com/ I'm using the same code, take a look on it, it works as expected – Saadi Toumi Fouad Aug 24 '20 at 16:53
  • @SaymoinSam, I checked this. But here there is no redirection inside the iframe. The initial URL is showing. For me, I am getting the initial page url only even after an iframe internal redirection. – Arun Aug 25 '20 at 03:27
  • @Arun ok I have updated the code to simulate that and it works – Saadi Toumi Fouad Aug 25 '20 at 10:33
  • @SaymoinSam, thanks a lot for the help. Still no success with this methods. Now I am using CustomEvent - addEventListener to communicate between the iframe and parent. And passing the values. It is working fine for me – Arun Aug 25 '20 at 12:37
  • Ok, I'm glad it finally worked – Saadi Toumi Fouad Aug 25 '20 at 16:41

0 Answers0