开发者

Access iFrame URL with JQuery / Javascript

开发者 https://www.devze.com 2023-01-16 04:55 出处:网络
I have a page like this... <div> <iframe id=\"mainContent\"> <iframe id=\"littleBox\">

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 开发者_如何学JAVAneed it to return the current url, so if someone navigates around it should return the current page they are on.


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

$("#mainFrame").get(0).contentWindow.location


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


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>


document.getElementById('mainContent').contentWindow.location.href
0

精彩评论

暂无评论...
验证码 换一张
取 消