0

Got a page. Div on top and iframe at the bottom. Need to pull a page url from iframe and put it into that div on top. As users go through pages in iframe, url in the top div must change accordingly.

I'm noob in JS and Jquery. So the question is can this be done? if so, can someone point me in right direction?

Thank you!

user3638287
  • 43
  • 1
  • 13

2 Answers2

0

You're probably looking for something along the lines of window.location However, in order to get the URL of the iFrame, you'll probably need to do a little bit more work. Same concept but you'll have to get the ID of the iFrame and use the window.location to get the location of the iFrame.

Here's another thread I found that may provide more value: Get current URL with JavaScript

Community
  • 1
  • 1
mwilson
  • 12,295
  • 7
  • 55
  • 95
0

If your script is in the iframe and you're trying to put the parent window's url into the div:

window.parent.$("#your-div").text(window.parent.location.href)

If your script is in the parent page and you're trying to put the frame's url into the div:

$(document).ready(function() {
   $("IFRAME").load(function() {
      $("#your-div").text($("IFRAME")[0].contentWindow.location.href);
   });
});

EDIT:

I should note that none of this will work unless the parent page and the framed page share the same origin.

SECOND EDIT:

Since your parent page and framed page are not the same origin, you technically can't do this. But you can get the "src" attribute for the iframe, if that is good enough. Note that this won't track with the frame as it changes pages. It will just be whatever page the iframe was created to show (if any).

$("IFRAME").attr("src")

Beyond that I don't think it is possible. It would clearly be a significant security problem. E.g: Make an iframe, make it full-window, track the URL and keep the location field in sync, and boom: you have a browser that can monitor someone's browsing, seeing parameters that should be encrypted and secure. This would be bad, and it would be super easy to trick someone into getting into a state like this.

gwcoffey
  • 5,551
  • 1
  • 18
  • 20