Access iFrame URL with JQuery / Javascript

巧了我就是萌 提交于 2019-12-21 03:01:32

问题


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.


回答1:


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

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



回答2:


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




回答3:


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>



回答4:


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


来源:https://stackoverflow.com/questions/3705594/access-iframe-url-with-jquery-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!