2

I have a page like this...

<div>
<iframe id="mainContent">
<iframe id="littleBox">
</div>

and I want to do this...

 $(".someButton").live('click', function() {
       alert(/*The URL That the Main Frame is ON*/);
 });

I found this: $("#mainFrame").get(0).location.href, but it doesnt work...

Also, I need it to return the current url, so if someone navigates around it should return the current page they are on.

Jason
  • 11,435
  • 24
  • 77
  • 131
  • Same question asked here: http://stackoverflow.com/questions/44359/how-do-i-get-the-current-location-of-an-iframe – epascarello Sep 14 '10 at 04:42

4 Answers4

14

Okay so you mean you have to access the URL of the parent from the child iframe?

$("#mainFrame").get(0).contentWindow.location
xar
  • 1,429
  • 2
  • 17
  • 29
  • The parent frame (untitled div in my example) needs to access the CURRENT url of the child iframe. The problem is that after naviagting to a different url from the starting url of the child iframe, I am still getting the original starting URL of the child iframe. – Jason Sep 15 '10 at 02:49
0

Does iframe.src not work? Or, are you trying to get the parent document? In that case, you can just use parent.

Azmisov
  • 6,493
  • 7
  • 53
  • 70
0

As Azmisov says iframe.src does the trick

<head runat="server">
    <title>iframe Test</title>
    <script type="text/javascript">
        function ShowURLs() {
            var control = document.getElementById("mainContent");
            alert("iframe source = " + control.src);
            alert("this page = " + location.href);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input type="button" id="someButton" value="Click Me" onclick="ShowURLs()" />
        <div>
            <iframe id="mainContent" src="Test.aspx" />
            <iframe id="littleBox" />
        </div>
    </div>
    </form>
</body>
JumpingJezza
  • 5,498
  • 11
  • 67
  • 106
  • 2
    No it doesn't. iframe.src returns the original url. In your case even if you navigate away from Test.aspx it will return Test.aspx as the src. Just add a link on Test.aspx to SomethingElse.aspx and then click the button again... The desired output would be SomethingElse.aspx. – Jason Sep 14 '10 at 03:06
0
document.getElementById('mainContent').contentWindow.location.href
j0k
  • 22,600
  • 28
  • 79
  • 90
JumpingJezza
  • 5,498
  • 11
  • 67
  • 106